如何第二次检索属性会话

时间:2016-04-17 19:34:42

标签: jsp session servlets attributes usebean

我想在我的网站上添加一张表格,然后使用添加的页面刷新页面。我使用一个对象("目录"),它包含所有图片并将其放在会话属性中。然后jsp文件检索此属性并显示此属性中的所有数据。我需要在" 添加servlet页面"更新对象"目录"随着新的图片。当我第一次添加照片时,它就像一个魅力。但重新加载" Home"页面,属性会话被自动删除..所以我不记得" 添加servlet页面"添加另一张图片。

我不知道这个属性在哪里删除了!

Home.java:

@WebServlet("/Home")
public class Home extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Catalogue catalogue = LectureCatalogue.read();
    request.getSession(true).setAttribute("catalogue", catalogue);
    this.getServletContext().getRequestDispatcher("/Catalogue.jsp").forward(request,response);
}

Catalogue.jsp:

<?xml version="1.0" encoding="utf-8" ?>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"  session="true" %>
<jsp:useBean id="catalogue" scope="session" class="catalogue.Catalogue"></jsp:useBean>
...
<form class="form-inline" action="Ajout" method="post" enctype="multipart/form-data">
...
</form>

Ajout.java:

@WebServlet("/Ajout")
public class Ajout extends HttpServlet {
    ...
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("catalogue")); //not null
        ... //I update the attribute session named "catalogue" here
        session.setAttribute("catalogue", catalogue);
        this.getServletContext().getRequestDispatcher("/Home").forward(request,response);

SessionAttributeListener返回此...:

[SessionAttr] Sun Apr 17 22:03:45 EEST 2016 Attribute added
[SessionAttr] Sun Apr 17 22:04:53 EEST 2016 Attribute replaced
[SessionAttr] Sun Apr 17 22:04:54 EEST 2016 Attribute removed

感谢您的帮助。

0 个答案:

没有答案