I have a problem with my servlet that inserts data into the database. I have an HTML form with the POST method that sends the data to the servlet, which in turn inserts the data and redirects to a different form. I have tried using the sendRedirect and RequestDispatcher functions, but it didn't work. When I tried to display an error message in the catch block, it jumps directly to that part. When I remove the redirects and just display a message that the data has been inserted, it works fine. However, as soon as I add the redirect, everything starts to malfunction. I have the necessary libraries since I have already used select and delete queries, and they worked without any issues. What should I do?
Here is my servlet that inserts 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(); } } }
and here is my JSP page:
<body>
*<div class="container">
* *<form method="POST" action="AjouterFacture">
* * *<div class="form-group">
* * * *<label for="numfac">Numéro de facture :</label>
* * * *<input type="text" id="nom" name="numfac" required>
* * *</div>
* * *<div class="form-group">
* * * *<label for="datefac">Date facture :</label>
* * * *<input type="date" id="telephone" name="datefac" required>
* * *</div>
* * *<div class="form-group">
* * * *<label for="choix">Mode Paiement :</label>
* * * *<select name="choix">
* * * * * *<option value="carte">Carte bancaire</option>
* * * * * *<option value="cheque">Cheque bancaire</option>
* * * * * *<option value="liquide">Liquide</option>
* * * *</select>
* * *</div>
* * *<div class="form-group">
* * * *<label for="id">Id client :</label>
* * * *<input type="number" id="email" name="id" required>
* * *</div>
* * *<button class="btn-submit" type="submit">OK</button>
* *</form>
*</div>
</body>