从servlet向java类发送参数
我有一个Jsp页面和servlet,它基本上接受城市数的值,然后接受矩阵的值。我的servlet将输入值作为字符串,将其转换为整数并存储在二维数组中 Index.jsp从servlet向java类发送参数,java,jsp,web,servlets,jakarta-ee,Java,Jsp,Web,Servlets,Jakarta Ee,我有一个Jsp页面和servlet,它基本上接受城市数的值,然后接受矩阵的值。我的servlet将输入值作为字符串,将其转换为整数并存储在二维数组中 Index.jsp 旅行商问题 提交 Servlet 公共类示例扩展了HttpServlet{ 受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{ int city=Integer.parseInt(request.
旅行商问题
提交
Servlet
公共类示例扩展了HttpServlet{
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
int city=Integer.parseInt(request.getParameter(“cities”);
字符串编号=request.getParameter(“矩阵”);
String[]splitText=numbers.split(“”);
int[]mat=新int[splitText.length];
for(int i=0;i
现在我想把这些参数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
{
公共类tspnearestness
{
私有int numberOfNodes;
私有堆栈;
公共TSPNearestNeighbor()
{
堆栈=新堆栈();
}
公共无效tsp(整数邻接矩阵[][])
{
numberOfNodes=邻接矩阵[1]。长度-1;
int[]已访问=新
int[numberOfNodes+1];
访问量[1]=1;
堆栈推送(1);
int元素,dst=0,i;
int min=整数最大值;
布尔minFlag=false;
系统输出打印(1+“\t”);
而(!stack.isEmpty())
{
元素=stack.peek();
i=1;
最小值=整数。最大值;
while(i 1&&访问[i]==0)
{
if(min>邻接矩阵[element][i])
{
min=邻接矩阵[元素][i];
dst=i;
minFlag=true;
}
}
i++;
}
if(minFlag)
{
访问量[dst]=1;
堆栈推送(dst);
系统输出打印(dst+“\t”);
minFlag=false;
持续
}
stack.pop();
}
}
公共静态void main(字符串…参数)
{
节点的整数个数;
扫描器=空;
尝试
{
节点数=城市//这里我想传递城市参数
int邻接矩阵[][]=新int[u节点数+1][u节点数+1];
对于(inti=1;i我在您的代码中看到一些逻辑错误,但我不打算进入该上下文,也不知道您(在处理方面)正在尝试做什么。但我将尝试涵盖您提出的实际问题,例如“如何将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);
}
}
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类:tspnearestnessneighbor.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);
}
}
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");
}
}
}
package com.server.testing;
导入java.util.InputMismatchException;
导入java.util.Stack;
公共类TSPNearestNear{
私有int numberOfNodes;
私有堆栈;
公共TSPNearestNeighbor(){
堆栈=新堆栈();
}
公共无效tsp(整数邻接矩阵[][]){
numberOfNodes=邻接矩阵[1]。长度-1;
int[]已访问=新int[numberOfNodes+1];
访问量[1]=1;
堆栈推送(1);
int元素,dst=0,i;
int min=整数最大值;
布尔minFlag=false;
系统输出打印(1+“\t”);
而(!stack.isEmpty()){
元素=stack.peek();
i=1;
最小值=整数。最大值;
while(i 1&&访问[i]==0){
if(min>邻接矩阵[element][i]){
min=邻接矩阵[元素][i];
dst=i;
minFlag=true;
}
}
i++;
}
if(minFlag){
访问量[dst]=1;
堆栈推送(dst);
系统输出打印(dst+“\t”);
minFlag=false;
持续
}
stack.pop();
}
}
公共静态void main(字符串[]args){
如果(args.lengthWhere)为“程序”运行?你想在哪里传递它们?实际上,这就是我困惑的地方。如果我通过在intellij中创建单独的.java文件来单独运行程序,然后运行。通过接受命令行输入工作正常。所以我想要一个web界面,我的进度是这样的。那么如何将这些输入的参数准确地传递给程序呢?那么,plJonathan我已经突出显示了我想要传递参数的地方,请根据我的逻辑检查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);
}
}
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");
}
}
}