38

So I am trying to get a servlet to add a Java object to the session of the user, when this servlet is requested. But after the servlet redirects to the next page and I try to retrieve the object, I get a null object instead.

Here is what I do to add the object to the HttpSession (in the servlet):

request.setAttribute("object", obj);

Then I try to retrieve it by (in the JSP):

 Object obj = request.getAttribute("object");

So how would I get obj to not be null?

Update: I have also tried this with nothing:

HttpSession session = request.getSession();
session.setAttribute("object", obj);

with the following in the JSP:

 Object obj = request.getSession().getAttribute("object");

Both ways still return null.

Tamer
  • 1,724
  • 4
  • 16
  • 15
  • 4
    You are setting to HttpRequest. But the question says HttpSession. Which one are you trying to do? – CoolBeans Apr 23 '11 at 19:57
  • Also make sure that at the top of your JSP you have: <%@page language="java" session="true" %> – Romain Hippeau Mar 07 '13 at 15:47
  • Is the obj null? I found something I couldn't get any objects to resolve from request.getSession(), I have to create another session object on my second page, and I needed to do session.getAttribute("object"); instead. – meltdownmonk Oct 08 '13 at 20:27
  • this Link is Helpful https://stackoverflow.com/questions/123657/how-can-i-share-a-variable-or-object-between-two-or-more-servlets?answertab=votes#tab-top – Mojtaba Jun 08 '17 at 04:06

4 Answers4

50

You are not adding the object to the session, instead you are adding it to the request.
What you need is:

HttpSession session = request.getSession();
session.setAttribute("MySessionVariable", param);

In Servlets you have 4 scopes where you can store data.

  1. Application
  2. Session
  3. Request
  4. Page

Make sure you understand these. For more look here

milchreis
  • 79
  • 7
Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
  • 6
    and you can get the session from request.getSession() – MeBigFatGuy Apr 23 '11 at 19:58
  • `<%Object obj = request.getAttribute("object");%>` – Tamer Apr 23 '11 at 20:03
  • 12
    @Tamer: Have you **read** this line of code? It says `request`, it doesn't say `session`. If you have set the attribute on the `session`, then you need to get it from the `session` as well! – BalusC Apr 23 '11 at 20:08
  • Yes, but I have also tried: `<%Object obj = request.getSession().getAttribute("object");%>`. However, neither worked for me. – Tamer Apr 23 '11 at 20:11
  • 1
    @Romain Hippeau can you post some useful link how data can be stored in session, application, request scope ? – Sadanand Jul 28 '14 at 11:50
13

Add it to the session, not to the request.

HttpSession session = request.getSession();
session.setAttribute("object", object);

Also, don't use scriptlets in the JSP. Use EL instead; to access object all you need is ${object}.

A primary feature of JSP technology version 2.0 is its support for an expression language (EL). An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page author to access a bean using simple syntax such as ${name} for a simple variable or ${name.foo.bar} for a nested property.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
7

Here you can do it by using HttpRequest or HttpSession. And think your problem is within the JSP.

If you are going to use the inside servlet do following,

Object obj = new Object();
session.setAttribute("object", obj);

or

HttpSession session = request.getSession();
Object obj = new Object();
session.setAttribute("object", obj);

and after setting your attribute by using request or session, use following to access it in the JSP,

<%= request.getAttribute("object")%>

or

<%= session.getAttribute("object")%>

So seems your problem is in the JSP.

If you want use scriptlets it should be as follows,

<%
Object obj = request.getSession().getAttribute("object");
out.print(obj);
%>

Or can use expressions as follows,

<%= session.getAttribute("object")%>

or can use EL as follows, ${object} or ${sessionScope.object}

Chathuranga Withana
  • 862
  • 1
  • 9
  • 12
2

The request object is not the session.

You want to use the session object to store. The session is added to the request and is were you want to persist data across requests. The session can be obtained from

HttpSession session = request.getSession(true);

Then you can use setAttribute or getAttribute on the session.

A more up to date tutorial on jsp sessions is: http://courses.coreservlets.com/Course-Materials/pdf/csajsp2/08-Session-Tracking.pdf

ditkin
  • 6,774
  • 1
  • 35
  • 37
  • I am having a similar problem, using the EL, i can access attributes from the application and request scopes, but not the session scope. I am using the proper syntax. – kiwicomb123 Sep 18 '16 at 02:07