0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3185672
  • 69
  • 1
  • 7
  • After reading three times, I gather that you actually want to set the session attribute in `Home` servlet only if it does not exist, is this true? I.e. `if (session.getAttribute("catalogue") == null) { session.setAttribute("catalogue", catalogue); }` Or do you want to collect them all in some `List` instead? – BalusC Apr 17 '16 at 19:38
  • In the Ajout.java file, the session attribute is replaced as I want. But when I reload the 'Home' servlet to refresh the page, the attribute session is automatically removed.. – user3185672 Apr 17 '16 at 20:06

0 Answers0