从一个servlet向另一个servlet发送一个值(例如id)(jsp + servlet + mysql)

时间:2014-08-27 08:52:37

标签: java jsp servlets insert

我有两张表 AFFILIATE idaff (自动增量),姓名,地址......)和他的 COTISATION ID 下,值,..)

  

我制作了两个单独的jsp表单和两个单独的Servlet,每个表单   从jsp表单收集信息并将其插入相关表格。

我希望能够选择/显示AFFILIATE和他受影响的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.

感谢您的建议。

Servlet代码:

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

1 个答案:

答案 0 :(得分:1)

在servlet GestAffiliate.java

中设置请求中idaff的值
request.setAttribute("idaff",idaff);

并将请求发送到jsp“Cotisation.jsp”并使用

在jsp中的隐藏参数中设置值
<input type="hidden" name="idaff" value=${requestScope.idaff}>

并且此jsp转发到您的第二个servlet,您可以使用

检索该值
request.getParameter("idaff")

你必须在jsp页面顶部的jsp页面上添加它

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

因为在jstl中提供了requestcope,但它可以正常工作