nabsterz
nabsterz

Reputation: 25

Sending a value (id for example) from one servlet to another (jsp+servlet+mysql)

I have two tables an AFFILIATE (idaff(AutoIncrement),name,address,..) and his COTISATION(id,value,..)

I made two separate jsp forms and two separate Servlets, each one collects info from a jsp form and inserts it into the concerned table.

I would like to be able to Select/Show the AFFILIATE and his affected COTISATION(s),

I wanted to use idaff as a foreign key in COTISATION table, but i don't know how
 to retrieve it from the first servlet (GestAffiliate.java) to the next servlet 
(GestCotisation.java) to be able to insert it in COTISATION Table.

Thank you for your suggestions.

Servlet Code :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out =response.getWriter();
    /*response.setContentType("text/html");
    out.println("<HTML>");
    out.println("<HEAD> <TITLE> TATATATATA </TITLE></HEAD>");
    out.println("</HTML>"); */

    Affilie newaff=new Affilie();
    newaff.setNom(request.getParameter("nom"));
    newaff.setPrenom(request.getParameter("prenom"));
    newaff.setNumaffiliation(request.getParameter("numaffiliation"));
    newaff.setCin(request.getParameter("cin"));

    newaff.setPpr(request.getParameter("ppr"));
    newaff.setNumaffilregimebase(request.getParameter("numaffregbase"));
    newaff.setMatriculeemployeur(request.getParameter("matriculeemployeur"));
    newaff.setNumpension(request.getParameter("numpension"));

    newaff.setDatenaissance(request.getParameter("datenaissance"));
    newaff.setTelephone(request.getParameter("telephone"));
    newaff.setAdresse(request.getParameter("Adresse"));
    newaff.setVille(request.getParameter("ville"));

    newaff.setCodepostal(request.getParameter("codepostal"));
    newaff.setEtatcivil(request.getParameter("etatcivil"));
    newaff.setRegime(request.getParameter("regime"));       

    Connection connection=null;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    try{
        connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/authentification","root","");
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
    try {
        Statement statement=connection.createStatement();
        statement.executeUpdate("INSERT INTO `affilie` VALUES (0,'"+newaff.getNom()+"','"+newaff.getPrenom()+"','"+newaff.getNumaffiliation()+"','"+newaff.getCin()+"','"+newaff.getPpr()+"','"+newaff.getNumaffilregimebase()+"','"+newaff.getMatriculeemployeur()+"','"+newaff.getNumpension()+"','"+newaff.getDatenaissance()+"','"+newaff.getTelephone()+"','"+newaff.getAdresse()+"','"+newaff.getVille()+"','"+newaff.getCodepostal()+"','"+newaff.getEtatcivil()+"','"+newaff.getRegime()+"')");
    // ('"+newaff.getNom()+"','"+newaff.getPrenom()+"','"+newaff.getNumaffiliation()+"','"+newaff.getCin()+"','"+newaff.getPpr()+"','"+newaff.getNumaffilregimebase()+"','"+newaff.getMatriculeemployeur()+"','"+newaff.getNumpension()+"','"+newaff.getDatenaissance()+"','"+newaff.getTelephone()+"','"+newaff.getAdresse()+"','"+newaff.getVille()+"','"+newaff.getCodepostal()+"','"+newaff.getEtatcivil()+"','"+newaff.getRegime()+"'
        request.getRequestDispatcher("Cotisation.jsp").forward(request, response);

    }
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    out.println(" AFFiliation PERFECTLY DONE ! ");
}
enter code here

Upvotes: 0

Views: 1227

Answers (1)

kirti
kirti

Reputation: 4609

set value of idaff in request in your servlet GestAffiliate.java

request.setAttribute("idaff",idaff);

and dispatch the request to jsp"Cotisation.jsp" and set the value in an hidden parameter there on the jsp using

<input type="hidden" name="idaff" value=${requestScope.idaff}>

and this jsp forwards to your second servlet where you can retrieve the value using

request.getParameter("idaff")

you will have to add this on your jsp page at the top of jsp page

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" 
    prefix="c" %>  

as requestscope is offered in jstl..but it will work

Upvotes: 1

Related Questions