I want to put a form to add a picture on my website and refresh the page with the added page. I use an object ("catalogue") which contain all the pictures and put it in a session attribute. Then the jsp file retrieve this attribute and show all the data in this attribute. I need this attribute on the "add servlet page" to update the object "catalogue" with the new picture. When I first add a photo, it works like a charm. But after reload the "Home" page, the attribute session is automatically removed.. So I can't recall the "add servlet page" to add another picture.
I don't know where is this attribute removed !
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);
The SessionAttributeListener returns this... :
[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
Thanks for your help.