Servlet sendRedirect/RequestDispatcher not working

mariap

Mitglied
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?

Voici ma servlet qui insère des données dans la base de données :
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();
}
}
}

et ici sur la page JSP :
<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>
 

Robert Zenz

Top Contributor
Code should be formatted as such/put into code tags. Otherwise it will become hard to read or even unreadable.

That said, "everything starts to malfunction" is a bad error description. What is the behavior you're seeing? What is the error you're getting?
 

Nouser

Mitglied
If an exception is thrown you end in the catch block and that is it.
Maybe use logging to see problems. Or try to rethrow exceptions you catch instead of printing the stacktrace (probably to nowhere)
Java:
throw new ServletException(e);

Then you at least know what's going on (eventually no sql connection? But at the moment that's all just guessing)
 

Ähnliche Java Themen

Neue Themen


Oben