2017-02-26 38 views
0

的问题,当我填写它调用servlet,它显示了正确的信息的形式,当用户单击后退按钮似乎调用页面上的servlet用值为null。我如何使它重新加载页面,以便用户可以在表单中填充。的Java Servlet重定向到页面自动调用其形式的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。

回答

-1

尝试使用GET而不是POST 的这可能会解决你的问题

+0

以前我试过这个,当我更改GET时发生这个错误。 HTTP状态405 - 此方法不支持HTTP方法GET –

+1

您可以覆盖servlet中的get方法并调用doPost(req,res);在重写的doGet()方法内部 – visrey

-1


通过使用您的形式POST方法可能会遇到这个问题。正如@visray建议的那样,您需要覆盖doGet() Servlet方法以使GET方法正常工作。

POST应该在处理数据库更改时使用方法,所以在你的情况下GET是合适的。