1

I have problem when I submit my form to insert data the URL can't change and when I refresh it, the data reinsert

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String _1 = request.getParameter("company_name").toString();
    String _2 = request.getParameter("city").toString();
    String _3 = request.getParameter("state").toString();
    String _4 = request.getParameter("zipcode").toString();
    String _5 = request.getParameter("branch").toString();
    String _6 = request.getParameter("address").toString();
    Database db = (Database) getServletContext().getAttribute("db");
    try {
        String sql = "insert into company(company_name,city,state,zipcode,branch,company_address) values('"+_1+"','"+_2+"','"+_3+"','"+_4+"','"+_5+"','"+_6+"')";
        db.updateSql(sql);
    } catch (Exception e2) {
        System.out.println(e2);
    }
    getServletContext().getRequestDispatcher("/company.jsp").forward(request, response);
}

1 Answers1

0

Your problem comes from the understanding of the forward method.

This method transfer the request and the response object to the new URL. It is invisible for the client's browser so the URL is unchanged. By reloading the page, you repeat your resquest so your sending again your data.

This behaviour is completely normal. If you want to redirect to another URL and have another request then you should use the sendRedirect method.

Refer to this post to have a complete description of both methods.

Community
  • 1
  • 1
RPresle
  • 2,078
  • 1
  • 21
  • 27