mercredi 21 novembre 2018

Send parameters to java program from servlet

I have a Jsp page and servlet which basically accepts the value for number of cities and next for matrix. My servlet takes the input value as string convert it into integer and store it in a two dimensional array

Index.jsp

<body>
<form action="sample" method="post">
    <h1>Travelling Salesman Problem</h1>
    <input placeholder="Number of Cities" type="text" name="cities" required="">
    <input placeholder="Matrix" type="text" name="matrix" required="">
    <button>Submit</button>
</form>
</body>

Servlet

public class sample extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int city = Integer.parseInt(request.getParameter("cities"));
        String numbers = request.getParameter("matrix");
        String[] splitText = numbers.split(" ");
        int[] mat = new int[splitText.length];
        for (int i = 0; i < splitText.length; i++) {
            mat[i] = Integer.parseInt(splitText[i]);

        }

        int array2d[][] = new int[city][city];
        int count = 0;
        for (int i = 0; i < city; i++) {
            for (int j = 0; j < city; j++) {

                if (count == mat.length)
                    break;
                array2d[i][j] = mat[i * city + j];
                count++;


            }

        }

    }
}

Now i want to pass these parameters city and array2d to the following java program

public class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour()
{
stack = new Stack<Integer>();
}
public void tsp(int adjacencyMatrix[][])
{
numberOfNodes = adjacencyMatrix[1].length - 1;
int[] visited = new
int[numberOfNodes + 1];
visited[1] = 1;
stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(1 + "\t");

while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes)
{
if
(adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{



if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
System.out.print(dst +"\t");
minFlag = false;
continue;
}


stack.pop();
}
}

public static void main (String... arg)
{
int number_of_nodes;
Scanner scanner = null;
try
{

number_of_nodes = city //here i want to pass the city parameter

int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes +1];



for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{

adjacency_matrix[i][j]= array2d[i][j] // and here the array 2d parameter

}
}
for (int i = 1; i <=number_of_nodes; i++)
{
for (int j = 1; j <=number_of_nodes; j++)
{
if
(adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{


adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
tspNearestNeighbour.tsp(adjacency_matrix);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input format");
}
scanner.close();
}
}

So how can we pass on these parameters from a servlet?




Aucun commentaire:

Enregistrer un commentaire