Problem when registering a product

3

The problem I have is that when I register, in this case a product, it registers normal but the problem is that it registers it 2 times, it registers everything well in the Database but what happens is that it sends it 2 times and Obviously an error will appear in the PRIMARY KEY because it is being sent twice.

This is the duplicate PK error:

"Duplicate entry 'P0001' for key 'PRIMARY'"

Here my servlet:

 /**
 * Servlet implementation class RegistroProductoServlet
 */
@WebServlet("/rp")
public class RegistroProductoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public RegistroProductoServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
procesar(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    procesar(request, response);

}

private void procesar(HttpServletRequest request, HttpServletResponse response) {
    String codprod = request.getParameter("txtCodigo");
    String fecharegistro = request.getParameter("txtFecha");
    String descripcion = request.getParameter("txtDescripcion");
    double precio = Double.parseDouble(request.getParameter("txtPrecio"));
    int tipo = Integer.parseInt(request.getParameter("cboTipo"));
    int stock = Integer.parseInt(request.getParameter("txtStock"));


    ProductoService ser = new ProductoService();

    ProductoDTO p = new ProductoDTO(codprod, fecharegistro, descripcion, tipo, stock, precio);
    int ok = ser.registrarProducto(p);

    if(ok!=0){
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.println("<html>");
            out.println("<head><title>Registro de Productos</title></head>");
            out.println("<body>");
            out.println("<h1>Producto Registrado!!!!!!!!!!!!!</h1>");   
            out.println("</body></html>");
        } catch (IOException e) {
            System.out.println("error aqui en el servlet de registro producto");
        }finally{
            out.close();
        }
    }else{
        RequestDispatcher rd = request.getRequestDispatcher("/registroproducto.jsp");
        try {
            rd.forward(request, response);
        } catch (ServletException e) {              
            e.printStackTrace();
        } catch (IOException e) {               
            e.printStackTrace();
        }
    }

}

}

    
asked by Jorge Requez 13.05.2016 в 03:16
source

1 answer

2

The problem is that the procesar method is being called twice. Here:

protected void doGet(/* ... */) throws ServletException, IOException {
    //...
    procesar(request, response);
}

protected void doPost(/* ... */) throws ServletException, IOException {
    //doGet llama a procesar
    doGet(request, response);
    //luego vuelves a llamar a procesar y se produce el error
    procesar(request, response);
}

Solution: Do not call the doGet method in the doPost method.

Even more important is that the methods doGet and doPost should not do the same . This is a serious problem that normally appears when you work with NetBeans. I recommend that you do not use the procesar method in both methods since each one has a different responsibility:

  • GET : Method to obtain server information. Usually get the query string data. You should not modify information that already exists on the server. It is usually used to navigate between the elements of a Web application and its resources.
  • POST : Method to process the information of the data sent. The data is usually in the body of the message. This method can (depending on the case) add, update or delete information.
answered by 13.05.2016 / 21:20
source