我想放置一个表单来在我的网站上添加图片并使用添加的页面刷新页面。我使用一个包含所有图片的对象(“目录”)并将其放入会话属性中。然后 jsp 文件检索该属性并显示该属性中的所有数据。我需要“添加 servlet 页面”上的这个属性来用新图片更新对象“目录”。当我第一次添加照片时,它就像一个魅力。但是重新加载“主页”页面后,属性会话被自动删除..所以我不记得“添加servlet页面”添加另一张图片。
我不知道这个属性在哪里被删除了!
主页.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);
}
目录.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
谢谢你的帮助。