如何将相同的参数从一个JSP传递到两个servlet?然后将不同的参数从servlet传递到同一个JSP?

时间:2022-05-04 07:43:04

I was new to the web application. my issue: how to pass the same parameter from one JSP to two servlets? Then pass the different parameters from servlet to the same JSP?

我是网络应用程序的新手。我的问题:如何将相同的参数从一个JSP传递给两个servlet?然后将不同的参数从servlet传递到同一个JSP?

important!! we should do the process A first then do the process B!!!!

重要!!我们应该首先进行流程A然后再进行流程B!

As the project takes too many processes, I would like to separate the processes into two servlet.

由于项目需要太多进程,我想将进程分成两个servlet。

Currently, I finish implement processA which is pass the search term from SEARCH PAGE JSP to SERVLET A (do the processA) and pass the result to WELCOME PAGE JSP. It works!!!(which i highlight in the red color in the picture)

目前,我完成了实现processA,它将搜索项从SEARCH PAGE JSP传递给SERVLET A(执行processA)并将结果传递给WELCOME PAGE JSP。它工作!!!(我突出显示在图片中的红色)

the code I used: Web.xml

我使用的代码:Web.xml

  <servlet>
    <servlet-name>ServletA</servlet-name>
    <servlet-class>test.processA</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletA</servlet-name>
    <url-pattern>/download result</url-pattern>
  </servlet-mapping>

Search page JSP:

搜索页面JSP:

<form   action="download result">           
             Please enter a Keyword <br> 
            <input type="text" name="term"size="20px">
            <input type="submit" value="submit">

</form>

servletA:

public class processA  extends HttpServlet { 
     protected void doGet(HttpServletRequest request, 
              HttpServletResponse response) throws ServletException, IOException 
          {
            // reading the user input

            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            // Retrieve search term from GET request and parse to desired format
            String searchTerm = (request.getParameter("term").toString()).replace("%20", "_").replace(" ", "_").replace("+", "_").replace(".", "");
            System.out.println("=====(servlet) searchTerm is:"+searchTerm);

  }

              protected void doPost(HttpServletRequest request, 
                      HttpServletResponse response) throws ServletException, IOException 
                  {
                 doGet(request, response);

          }  
                }

如何将相同的参数从一个JSP传递到两个servlet?然后将不同的参数从servlet传递到同一个JSP?

So how to implement the processB into the system??? which will look like the picture i showed.

那么如何将processB实现到系统中???这将看起来像我展示的图片。

the servletB

public class processB extends HttpServlet {
    protected void doGet(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException 
          {
            doPost(request,response);
          }


    protected void doPost(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException 
      {



        String searchTerm = (request.getParameter("term")).replace(" ", "_");
        String queryTerm = request.getParameter("term");

        System.out.println("=====(servlet) searchTerm is:"+searchTerm);

System.out.println("=====(servlet) keep doing the other process……………………!!!”);


}
}

thanks so much! or if the doGET and doPOST cannot be used at the same time, I can change the processA to doPost.

非常感谢!或者如果不能同时使用doGET和doPOST,我可以将processA更改为doPost。

important!! we should do the process A first then do the process B!!!!

重要!!我们应该首先进行流程A然后再进行流程B!

1 个解决方案

#1


2  

There can be only one action method(get/post etc.) for your form of SearchPage.jsp.

您的SearchPage.jsp形式只能有一种操作方法(获取/发布等)。

You as a programmer have to decide first what and how the request needs to be process instead of letting users to choose between doPost and doGet methods.

作为程序员,您必须首先决定请求需要处理的内容和方式,而不是让用户在doPost和doGet方法之间进行选择。

Both the methods have different purposes check the difference here

这两种方法都有不同的用途,请查看这里的区别

You have to keep both the processing units A and B into a single servlet(servletA/servletB),
For example: Call Process A first from JSP then call Process B from Process A and finally redirect/forward the response to Welcome.jsp from Process B
Below is the code:

您必须将处理单元A和B都保存到单个servlet(servletA / servletB)中,例如:首先从JSP调用Process A然后从Process A调用Process B,最后将响应重定向/转发到Process的Welcome.jsp B下面是代码:

Search.jsp

<form action="download result" method="get">
...
</form>

servletA:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
      {
        ... //processing logic of A
        ... //processing logic of A
        ... //processing logic of A
        doPost(request,response);//call Post 
      }
 protected void doPost(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException 
  {
        ... //processing logic of B
        ... //processing logic of B
        ... //processing logic of B

        RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");  
        rd.forward(request, response);  
  }

Note: you can do viceversa, that is call doPost first and later doGet as per your requirement. Also, need to change method="post" in form tag for this.

注意:您可以执行反之亦然,即首先调用doPost,然后根据您的要求调用doGet。此外,需要在表单标签中更改method =“post”。


For your query in comments use the below code:

对于您在评论中的查询,请使用以下代码:

servletA:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
      {
        ... //processing logic of A
        ... //processing logic of A
        ... //processing logic of A
        RequestDispatcher dispatcher = null;
        dispatcher=request.getRequestDispatcher("servletB");
        dispatcher.forward(request, response);//call Post 
      }

servletB:

 protected void doPost(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException 
  {
        ... //processing logic of B
        ... //processing logic of B
        ... //processing logic of B

        RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");  
        rd.forward(request, response);  
  }

#1


2  

There can be only one action method(get/post etc.) for your form of SearchPage.jsp.

您的SearchPage.jsp形式只能有一种操作方法(获取/发布等)。

You as a programmer have to decide first what and how the request needs to be process instead of letting users to choose between doPost and doGet methods.

作为程序员,您必须首先决定请求需要处理的内容和方式,而不是让用户在doPost和doGet方法之间进行选择。

Both the methods have different purposes check the difference here

这两种方法都有不同的用途,请查看这里的区别

You have to keep both the processing units A and B into a single servlet(servletA/servletB),
For example: Call Process A first from JSP then call Process B from Process A and finally redirect/forward the response to Welcome.jsp from Process B
Below is the code:

您必须将处理单元A和B都保存到单个servlet(servletA / servletB)中,例如:首先从JSP调用Process A然后从Process A调用Process B,最后将响应重定向/转发到Process的Welcome.jsp B下面是代码:

Search.jsp

<form action="download result" method="get">
...
</form>

servletA:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
      {
        ... //processing logic of A
        ... //processing logic of A
        ... //processing logic of A
        doPost(request,response);//call Post 
      }
 protected void doPost(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException 
  {
        ... //processing logic of B
        ... //processing logic of B
        ... //processing logic of B

        RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");  
        rd.forward(request, response);  
  }

Note: you can do viceversa, that is call doPost first and later doGet as per your requirement. Also, need to change method="post" in form tag for this.

注意:您可以执行反之亦然,即首先调用doPost,然后根据您的要求调用doGet。此外,需要在表单标签中更改method =“post”。


For your query in comments use the below code:

对于您在评论中的查询,请使用以下代码:

servletA:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
      {
        ... //processing logic of A
        ... //processing logic of A
        ... //processing logic of A
        RequestDispatcher dispatcher = null;
        dispatcher=request.getRequestDispatcher("servletB");
        dispatcher.forward(request, response);//call Post 
      }

servletB:

 protected void doPost(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException 
  {
        ... //processing logic of B
        ... //processing logic of B
        ... //processing logic of B

        RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");  
        rd.forward(request, response);  
  }