Redirect blocked on Java EE backend

mariaP -  
KX Posted messages 19031 Status Moderator -

Hello,
I have a problem regarding my servlet that inserts data into the database. I have an HTML form with the post method that sends data to the servlet, which in turn inserts the data and redirects to another different form. I tried using the sendRedirect function and RequestDispatcher but it didn’t work; when I tried to display an error message in the catch, it jumps directly there. I have the necessary libraries since I’ve already used select and delete queries and it worked without problems.
What should I do?

Here is my servlet that allows inserting the data into the database:

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class AjouterFacture extends HttpServlet {     public void doPost(HttpServletRequest requete, HttpServletResponse reponse)             throws ServletException, IOException {                 reponse.setContentType("text/html;charest=UTF-8");                 String numfac = requete.getParameter("numfac");                 String datefac = requete.getParameter("datefac");                 String choix = requete.getParameter("choix");                 int id = Integer.parseInt(requete.getParameter("id"));                          String sqlQuery = "INSERT INTO `facture`(`Num-facture`, `Date-facture`, `Mode-paiement`, `Id_client`) VALUES ('" + numfac + "', '" + datefac + "', '" + choix + "', '" + id + "')";             try {                 Class.forName("com.mysql.jdbc.Driver");                 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/facturation", "root", "");                 Statement stmt = con.createStatement();                 int r = stmt.executeUpdate(sqlQuery);                          if(r > 0){                 RequestDispatcher rd = requete.getRequestDispatcher("FormulaireAjouterLigneFacture.jsp");                 rd.forward(requete, reponse);                 }                 else{                 RequestDispatcher rd = requete.getRequestDispatcher("FormulaireAjouterFacture.jsp");                 rd.include(requete, reponse);                 PrintWriter out = reponse.getWriter();                 out.print("Insertion failed.");                 }                 } catch (Exception e) {                 e.printStackTrace();                 }             } }


I use phpMyAdmin XAMPP, NetBeans 8.2 and GlassFish as server.

1 answer

  1. KX Posted messages 19031 Status Moderator 3 020
     

    Hello,

    "when I tried to display an error message in the catch it jumps straight to the bottom"

    What is the error displayed by your e.printStackTrace()?


    0