问题是当我填写它调用servlet的表单时,它显示正确的信息,当用户单击后退按钮时,它似乎在该页面上调用值为null的servlet。如何制作它以便重新加载页面,以便用户可以重新填写表单。
SetTimeZone.xhtml
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SetTimeZone</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div>
<form name ="SetTimeZone" method="post" action="SetTimeZoneServlet">
Set Time Zone: <input type="text" name="timeZone"/><br></br><br></br>
<input type ="submit" value="Submit" name="submit"/>
</form>
</div>
</body>
public class SetTimeZoneServlet extends HttpServlet {
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
TimeZoneBean bean = new TimeZoneBean();
String city = request.getParameter("timeZone");
bean.setCity(city);
String temp = bean.checkCity();
String value = "";
if ("error".equals(temp)) {
value = "Sorry no information is availible for " + city;
} else {
value = "The current time in " + city + " is " + bean.getTime();
}
try (PrintWriter out = response.getWriter()) {
response.setContentType("text/html;charset=UTF-8");
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet OrderFormServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>" + value + "</p>");
out.println("<form name=\"SetTimeZone.xhtml\" method=\"post\" name=\""
+ "SetTimeZoneRedirectServlet\"> ");
out.println("<input type =\"submit\" value=\"Back\"/ name=\"back\">");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
public class SetTimeZoneRedirectServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("SetTimeZone.xhtml");
}
}
重定向到页面后得到的输出是; 对不起,没有关于null的信息。
答案 0 :(得分:-1)
尝试使用GET而不是POST 这可以解决您的问题
答案 1 :(得分:-1)
通过对表单使用POST
方法,您可能会遇到此问题。正如@visray建议的那样,您需要覆盖doGet()
Servlet方法才能使GET
方法正常工作。
POST
方法,因此在您的情况下GET
是合适的。