如何通过两个servlet将会话转发到jsp?

时间:2017-02-22 02:50:12

标签: java jsp session servlets forwarding

我正在尝试转发用户输入的数据,并从servlet中插入数据库,负责将数据插入数据库,再显示数据库结果的servlet,最后将其显示在jsp上。我在jsp中得到一个空的会话。用户会话attribut“sessionUtilisateur”运行良好,但不是项目attribut“projetdb”。我的代码出了什么问题?

用于在DB中插入数据的Servlet:

private final static String VUE = "/Affichage"; 
private final static String ATT_MESSAGES = "messages";
private final static String ATT_SESSION = "sessionUtilisateur";


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    HttpSession session = request.getSession();
    session.setMaxInactiveInterval(20 * 60);
    ConnectBDD test = new ConnectBDD();

    List<String> messages = test.insertNouveauProjet(request);
    String statut = test.testStatut(request);
    request.setAttribute("statut", statut);

    request.setAttribute(ATT_MESSAGES, messages);

    if (session.getAttribute(ATT_SESSION) == null) {
        response.sendRedirect(request.getContextPath() + "/Connexion");
    } else {
        this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
    }
}

用于显示数据的Servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");

    HttpSession session = request.getSession();
    session.setMaxInactiveInterval(20 * 60);
    ConnectBDD test = new ConnectBDD();

    List<Object> tachedb = test.selectTaches(request);
    String statut = test.testStatut(request);
    request.setAttribute("statut", statut);
    // Mettre les champs de formulaire de creation de projet dans un attribut

    // Mettre les messages de BDD dans un attribut
    List<Object> projetdb = test.selectDataProjet(request);

    if (session.getAttribute(ATT_SESSION) == null) {
        response.sendRedirect(request.getContextPath() + "/Connexion");
    } else {
        session.setAttribute("tachedb", tachedb);
        session.setAttribute("projetdb", projetdb);
        this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
    }

}

JSP:

    <c:if test="${!empty sessionScope.sessionUtilisateur}">
        <p>${sessionScope.projetdb} </p>
    </c:if>

0 个答案:

没有答案