2016-04-22 15 views
0

JSPとサーブレットを初めて使用しています。私はJSPの例(register.jsp)のフォームを持っていて、データベースに値を挿入するためにサーブレットにデータを送るのに役立ちます。しかし、正常に挿入した後、(register.jsp)と同じURLにヒットした場合、以前にデータベースに入力したのと同じデータが再度再送信されます。どのように私はこの場合を防ぐのですか?以下は私のコードダブルフォーム提出JSPサーブレット

JSP

<form action="ServletComment" method="post" class="form-inline" role="form"> 
     <div class="form-group"> 
      <input class="form-control" type="text" placeholder="Your comments" name="userComment" /> 
      <input type="hidden" name="Action" value="updateComment" /> 
     </div> 
     <div class="form-group"> 
      <button class="btn btn-default"> Add</button> 
     </div> 
    </form> 

サーブレット

String checkComment = null; 
    checkComment = request.getParameter("Action"); 

    if(checkComment.equals("updateComment")) 
    { 
     // my coding 
    } 

request.getRequestDispatcher("/register.jsp").forward(request,response); 

答えて

0

あなたは、おそらく一般的な方法であなたのservletで要求を受信して​​います。別のHTTP要求の異なる方法overrideする必要があります。 Like:

//here only http get request will go in this method 
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
      //do whatever you want 
     } 


    //here only http post request will go in this method  
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
      String checkComment = null; 
      checkComment = request.getParameter("Action"); 

      if(checkComment.equals("updateComment")) 
      { 
       // my coding 
      } 

      request.getRequestDispatcher("/register.jsp").forward(request,response); 
     } 
関連する問題