gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:44:44 25 4
gpt4 key购买 nike

我是网络应用程序的新手。我的问题:如何将相同的参数从一个 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!!!

最佳答案

SearchPage.jsp 表单只能有一种操作方法(获取/发布等)。

作为程序员,您必须首先决定需要处理什么内容以及如何处理请求,而不是让用户在 doPostdoGet 方法之间进行选择。

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

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

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。另外,需要为此更改 form 标记 中的 method="post"

<小时/>

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

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

关于java - 如何将相同的参数从一个 JSP 传递到两个 servlet?然后将不同的参数从servlet传递到同一个JSP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540277/

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