gpt4 book ai didi

java - 管理来自同一个 servlet 的两个 JSP 输出

转载 作者:行者123 更新时间:2023-12-02 02:51:04 25 4
gpt4 key购买 nike

在我的项目中,我有以下文件:

  • pole.jsp 包含表单和提交
  • results.jsp,我在其中显示结果(它只有标题)
  • 一个 PollServlet,我在 poll.jsp 和 results.jsp 中设置了标题

这里是文件:民意调查.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Poll Page</title>
</head>
<body>
<form action="/poll?action=pole" method="POST">
<div>
<a><h2><% out.print(request.getAttribute("oldTitle").toString());%>
</h2></a><br>
</div>
<br><br>
<input type="submit" name = "submit"value="submit">
</form>
</body

结果.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Results</title>
</head>
<body >
<form action="/poll?action=results" method="POST">
<a><% out.print(request.getAttribute("title"));%></a>
</form>
</body>
</html>

PollServlet.java

@WebServlet(name = "PollServlet", urlPatterns = {"/poll"})
public class PollServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {

}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("pole")) {
request.setAttribute("oldTitle","new tile for poll.jsp ");
getServletConfig().getServletContext().getRequestDispatcher(
"/poll.jsp").forward(request, response);
} else if (action.equals("results")) {

/* set the title for results.jsp */
request.setAttribute("title","title for results.jsp");
getServletConfig().getServletContext().getRequestDispatcher(
"/results.jsp").forward(request, response);

}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request,
}

问题似乎出现在 servlet 的 doGet() 方法中。我只能设置第一个项目 (poll.jsp) 标题的值,而不能设置第二个项目 (results.jsp) 的值,我做错了什么以及如何正确实现?谢谢!

最佳答案

您不能多次转发同一请求,因为一旦您转发了您的请求,您的响应就已经被送达。请参阅this问题。

但是,在 forward() 之后编写的代码仍将被执行,但它用于其他目的(例如日志记录...),但您无法再次转发该请求。

<小时/>

编辑

您可以向您的网址 /poll?action=pole/poll?action=results 发送一个参数,这将帮助您查明此页面是否poll.jspresults.jsp,类似这样的

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String action = request.getParameter("action");

if(action.equals("pole")) {

/* set the title for pole.jsp */

request.setAttribute("oldTitle","new tile for poll.jsp ");
getServletConfig().getServletContext().getRequestDispatcher(
"/poll.jsp").forward(request, response);

} else if(action.equals("results")){

/* set the title for results.jsp */
request.setAttribute("title","title for results.jsp");
getServletConfig().getServletContext().getRequestDispatcher(
"/results.jsp").forward(request, response);

}
}

关于java - 管理来自同一个 servlet 的两个 JSP 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43840337/

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