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();
}
}
}
}