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

时间:2016-11-11 02:33:45

标签: java jsp servlets web-applications

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

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

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

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

我使用的代码: 的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>

搜索页面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);

          }  
                }

enter image description here

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

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……………………!!!”);


}
}

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

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

1 个答案:

答案 0 :(得分:2)

SearchPage.jsp形式只能有一个方法(获取/发布等)动作。

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

两种方法都有不同的用途,请检查差异here

您必须将处理单元A和B都保存在一个servlet(servletA / servletB)中, 例如:首先从JSP调用Process A然后从Process B调用Process A,最后将响应重定向/转发到Welcome.jsp Process 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);  
  }

注意:您可以反之亦然,即根据您的要求,先调用doPost,然后再调用doGet。此外,还需要更改method="post"中的form tag

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

<强> 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);  
  }