我有一个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 //我要在此处传递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++)
{
adjacency_matrix [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一样。所以,请指导我。
答案 0 :(得分:1)
我在您的代码中看到了一些逻辑错误,但是我没有进入那种上下文,而且我不知道您要做什么(在处理方面)。但是,我将尝试解决您所提出的实际问题,例如“如何将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");
}
}
}