2017-02-26 6 views
0

正しい情報を表示するサーブレットをフォームに入力するときに問題が発生する。ユーザーが戻るボタンをクリックすると、そのページのサーブレットを呼び出すようだ値nullユーザーがフォームに補充できるように、ページをリロードするようにするにはどうすればいいですか?Javaサーブレットのページへのリダイレクトがフォームサーブレットを自動的に呼び出す

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

これは、あなたがこの問題が発生する可能性があり、あなたのフォームのPOST方法を使用することにより、あなたの問題

+0

Iveは以前これを試しました.GETをPOSTに変更するとこのエラーが発生します。 HTTPステータス405 - HTTPメソッドGETはこのURLでサポートされていません –

+1

サーブレットのgetメソッドをオーバーライドしてdoPost(req、res)を呼び出すことができます。オーバーライドされたdoGet()メソッドの内部 – visrey

-1


を解決することがGETの代わりにPOST を使用してみてください。 @visrayが示唆するように、GETメソッドが正しく動作するように、doGet()サーブレットメソッドをオーバーライドする必要があります。

POSTデータベースの変更を扱う際には、GETが適切です。

関連する問題