Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: sendRedirect/RequestDispatcher not working

  1. #1
    Junior Member
    Join Date
    May 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default sendRedirect/RequestDispatcher not working

    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>

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,012
    Thanks
    63
    Thanked 2,705 Times in 2,655 Posts

    Default Re: sendRedirect/RequestDispatcher not working

    Also posted here: https://coderanch.com/t/772828/java/...atcher-working
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: September 25th, 2014, 03:21 PM
  2. RequestDispatcher include()
    By Shikhir in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 15th, 2013, 12:40 PM
  3. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM

Tags for this Thread