2017-02-26 36 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是合適的。