views:

910

answers:

2

I have several .jsp pages and several servlets.

I need to save some information to a session variable. In the jsp page I simply refer to

session.get...()

or

session.set...()

Without explicitly declaring an HttpSession object.

But in the servlet the proper rules of programming apply and I have to create an object first.

My concern is if I create an object like this

HttpSession session = new HttpSession();

and then write to it using something like this

session.setAttribute("files",fileList);

my concern is that I am not writing to the same session object that was being referenced in the .jsp file.

What do I do so that I can write to the same object in any jsp or servlet.

+2  A: 

To preprocess data, use Servlet's doGet() method.

Data data = dataDAO.load();
request.setAttribute("data", data);
request.getRequestDispatcher("page.jsp").forward(request, response);

To access data in JSP, use EL (which searches in page, request, session and application scope in this order for attributes with the given name).

<br>Plain object: ${data}
<br>A property: ${data.property}
<br>Explicitly search in request scope: ${requestScope.data}

To send data from JSP to servlet you normally use request parameters which are controlled by the client side. Most commonly HTML forms are been used for this. Alternatively you can also use Javascript to fire an asynchronous request to the server side.

Anything in a certain scope is accessible for anything which lives in the same scope. The request scope lives from the moment that the client initiated a request (by clicking a link, button, bookmark or by entering the URL in address bar) until the moment that the server has sent the last bit of the response. You normally store request specific data in there like form data. The session scope lives from the moment that the client requested the webpage for the first time and the HttpSession hasn't been created yet until the time that the HttpSession times out after not been used for a time which is configureable as in web.xml, or when the code explicitly times out it using HttpSession#invalidate(). You normally store user-specific data in there, like the logged in user and user preferences and so on. The application scope lives from the moment that the server starts up until the moment that the server shutdowns (or restarts). You normally store applicationwide data in there, like static dropdown data, the DAO factory, webapp configuration data, etcetera.

The request is accessible by HttpServletRequest argument in Servlet class.
The session is accessible by HttpServletRequest#getSession() in Servlet class.
The application is accessible by inherited getServletContext() method in Servlet class.
They all have a get/setAttribute() method.

To learn more about JSP/Servlet/EL I can recommend you the Sun Java EE 5 tutorial part II chapters 1-8.

Good luck.

BalusC
I think what you are saying is that I use the servlet not to save to the session object diretly, but to pass the data to the jsp which saves to the session object. That sounds fair enough.
Ankur
You should not use scriptlets in JSP. That logic belongs in a Java class like servlet/dao/bean/utilclass/etc. Use taglibs/EL in JSP only. Good luck.
BalusC
+5  A: 

You need to obtain the HttpSession by calling the HttpServletRequest.getSession() method.

The HttpServletRequest is passed in to your doGet() method. If no session was already in place for this request, then getSession() will create one. If a session is already in place and associated with this request, then getSession() will retrieve the existing one instead. If you use this standard mechanism, then you will automatically share the same session between your JSPs and servlets.

You should never be trying to construct an HttpSession directly, as it is managed by the container. Indeed, you cannot create a new one by calling new HttpSesion() because HttpSession is merely an interface and cannot be instantiated.

alasdairg
Oh yes, that was the actual question. The HttpSession is indeed to be obtained by HttpServletRequest#getSession(). +1.
BalusC