title: Servlet cancel post request in refresh
body: <p>I have the following:</p>
  
  <ul>
  <li>JSP page submits a request to a Servlet to add a new customer</li>
  <li>Servlet uses some Action class</li>
  <li>redirect to some other Jsp page</li>
  </ul>
  
  <p>Here is the source code.</p>
  
  <p><strong>new_customer.jsp:</strong></p>
  
  <pre><code>&lt;form action="/NewCustomerServlet" method="post"&gt;
  
    &lt;input type="test" name="company_name" /&gt;
    &lt;input type="submit" name="save_button" value="Save"/&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>NewCustomerServlet:</strong></p>
  
  <pre><code>@Override
  protected void doPost(HttpServletRequest request, response) throws ServletException, IOException {
  if(request.getParameter("save_button") != null){
      Customer customer;
      try {
          customer = action.createCustomer(request, response);
          request.setAttribute(Const.CUSTOMER, customer);
          RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp?v=v_cst");
          dispatcher.forward(request, response);
          return;
      } catch (InstantiationException | IllegalAccessException
                      | ClassNotFoundException | SQLException e) {
          e.printStackTrace();
  
          request.setAttribute(Const.ERR_MSG_ATTR_NAME, "Failed to insert new customer: " +
                               e.getMessage());
          RequestDispatcher dispatcher = request.getRequestDispatcher("CRM/index.jsp?v=n_cst");
          dispatcher.forward(request, response);
          return;
      }    
  }
  </code></pre>
  
  <p><strong>index.jsp</strong>:</p>
  
  <pre><code>&lt;%
      if(request.getParameter("v").equals("v_cst")) {%&gt;
          &lt;jsp:include page="customer/view_customer.jsp"&gt;&lt;/jsp:include&gt;
  &lt;%} %&gt;
  </code></pre>
  
  <p><strong>view_customer.jsp</strong>:</p>
  
  <pre><code>&lt;%
      Customer customer = (Customer)request.getAttribute(Const.CUSTOMER);
  
      String customerId = "";
      String name = "";
      String phone = "";
      String website = "";
      String address = "";
  
      if(customer != null){
  
          customerId = customer.getCustomerId();
          name = customer.getName();
          phone = customer.getPhone();
          website = customer.getWebsite();
          address = customer.getAddress();
      }
  %&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;label&gt;Customer ID&lt;/label&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" name="customer_id" value="&lt;%=customerId %&gt;" /&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;input type="button" value="Search" onclick="javascript:searchCustomer"/&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;label name="search_customer_err_msg" value="" style="color: red;"&gt;&lt;/label&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
          &lt;td&gt;
              &lt;label&gt;Customer Name&lt;/label&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" name="customer_name"  value="&lt;%= name%&gt;"/&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;label&gt;Customer website&lt;/label&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" name="customer_website" value="&lt;%= website%&gt;" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
          &lt;td&gt;
              &lt;label&gt;Customer phone&lt;/label&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" name="customer_phone" value="&lt;%= phone%&gt;"/&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;label&gt;Customer Address&lt;/label&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" name="customer_address" value="&lt;%= address%&gt;"/&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>After adding a new customer from the page new_customer.jsp, and viewing the page view_customer.jsp in the browser, if I refresh the page (from the page where I see the view_customer.jsp) it will submit the data again to the servlet and add a new customer and I will see the same data with a new customer id.</p>
  
  <p>Maybe I should mention also that i see in the browser address bar, the URL of the <code>NewCustomerServlet</code> and not of the index page.</p>
  
  <p>Anyone knows what I missed here to cancel the post again in refresh?</p>
  
  <p>** Maybe i forgot to mention that also the new_customer.jsp is also included in the index.jsp page, maybe this could be the reason of this behaviour?</p>