gpt4 book ai didi

java - 从 servlet 向 java 类发送参数

转载 作者:行者123 更新时间:2023-12-02 10:34:09 25 4
gpt4 key购买 nike

我有一个 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 //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();
}
}

那么我们如何从 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");
}

}
}

关于java - 从 servlet 向 java 类发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53415305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com