【问题标题】:Send parameters to java class from servlet从servlet向java类发送参数
【发布时间】:2019-04-24 05:07:43
【问题描述】:

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

索引.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>

小服务程序

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。所以请指导我完成这个。

【问题讨论】:

  • 那个“程序”在哪里运行?
  • 你想把它们传到哪里?
  • 实际上这就是我感到困惑的地方。如果我通过在 intellij 中创建单独的 .java 文件来单独运行程序并运行。通过接受命令行输入可以正常工作。所以我想要一个网络界面,我的进步是这样的。那么如何将这些输入的参数准确地传递给程序呢?所以,请指导我。
  • @Jonathan 我已经突出显示了我要传递参数的地方,请检查

标签: java jsp web servlets jakarta-ee


【解决方案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");
            }

    }
}

【讨论】:

  • 根据我的逻辑,java 类工作正常。我只是想知道有关传递值​​的信息。所以,我会试试你解释的,让我看看。
  • 好的,如果有效,请告诉我(如果有效,您可以接受答案)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 2014-07-21
相关资源
最近更新 更多