从servlet向java类发送参数



我有一个Jsp页面和servlet,它基本上接受城市数量的值和矩阵的下一个值。我的servlet将输入值作为字符串,将其转换为整数并存储在二维数组中

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++;

}
}
}
}

现在,我想将这些参数city和array2d传递给下面的java类

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//这里我想传递城市参数

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++)
{

邻接矩阵[i][j]=array2d[i][j]//,这里是数组2d参数

}
}
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();
}
}

那么,我们如何从servlet传递这些参数呢?这门课在哪里?在servlet本身中,或者创建一个单独的类,就像我使用intellij一样。所以请引导我度过难关。

我在您的代码中看到了一些逻辑错误,但我不会进入该上下文,我不知道您要做什么(在处理方面(。但是,我将尝试涵盖您所问的实际问题,比如"如何将jsp参数传递给您的另一个类"。

因此,您可以直接将字符串作为参数传递给另一个类,如我在下面的代码中所示。我想我不需要进一步解释,你可以看到你会得到的代码。

首先您的Servlet代码:Sample.java

package com.server.testing;
import java.io.IOException;
public class Sample extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String city = request.getParameter("cities");
String numbers = request.getParameter("matrix");
String[] args = new String[2];
args[0] = city;
args[1] = numbers;
TSPNearestNeighbour.main(args);
}
}

现在您的另一个java类:TSPNearestNeighbour.java

package com.server.testing;
import java.util.InputMismatchException;
import java.util.Stack;
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[] args) {
if(args.length<2) {
System.out.println("Two arguments required <city> <numbers>");
System.exit(-1);
}
int number_of_nodes=Integer.parseInt(args[0]);
String[] splitText = args[1].split(" +");
int[] mat = new int[splitText.length];
for (int i = 0; i < splitText.length; i++) {
mat[i] = Integer.parseInt(splitText[i]);
}

try {
int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
int count = 0;
for (int i = 1; i <= number_of_nodes; i++) {
for (int j = 1; j <= number_of_nodes; j++) {
if (count == mat.length)
break;
adjacency_matrix[i][j]=  mat[(i-1) * number_of_nodes + (j-1)];
count++;
}
}
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");
}
}
}

最新更新