39

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.

4
  • 4
    You are setting to HttpRequest. But the question says HttpSession. Which one are you trying to do? Commented Apr 23, 2011 at 19:57
  • Also make sure that at the top of your JSP you have: <%@page language="java" session="true" %> Commented Mar 7, 2013 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. Commented Oct 8, 2013 at 20:27
  • this Link is Helpful stackoverflow.com/questions/123657/… Commented Jun 8, 2017 at 4:06

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

5 Comments

and you can get the session from request.getSession()
<%Object obj = request.getAttribute("object");%>
@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!
Yes, but I have also tried: <%Object obj = request.getSession().getAttribute("object");%>. However, neither worked for me.
@Romain Hippeau can you post some useful link how data can be stored in session, application, request scope ?
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.

Comments

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}

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.