0

Good afternoon, I'm trying to recover an instantiated object from a servlet in the HttpSession object.

When I try to recover from the JSP, I get the object smoothly. However when I try to retrieve this data from another Servlet, not me gets the object, It returns me NULL directly, although the session ID is the same.

Here's the code where I instantiate the object to pass:

request.getSession(true);
request.getSession().setAttribute("object1", object1);

This is the code that attempt to retrieve the object.

req.getSession().getAttribute("object1");

Can you think of anything?

Thanks and regards.

user2606850
  • 485
  • 4
  • 12

2 Answers2

0

In jspkeep the data in session like this way

session.setAttribute("object1", object1); and retrieve in servlet like this way

HttpSession session=request.getSession();
session.getAttribute("object1");
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • session.setAttribute() is different from request.getSession() in jsp – SpringLearner Dec 03 '13 at 12:31
  • they are different i know but does it causing the issue here? `true` is just creating a new session if not present as it has been created so it should not create the issue. – Trying Dec 03 '13 at 12:32
  • @Trying Then we have to wait till OP leaves a comment whether my answer works or not – SpringLearner Dec 03 '13 at 12:33
  • i am removing my down vote but i am sure it is not contributing to the issue. – Trying Dec 03 '13 at 12:35
  • @Trying if it does not work then i will better delete my answer but I am sure cookies are not disabled otherwise getting in jsp is also not possible – SpringLearner Dec 03 '13 at 12:36
-1

Try this

Listing 3: Storing an Object

public class logonServlet extends HttpServlet{

public void service(HttpServletRequest _req, HttpServletRe-

sponse _res) throws ServletException{

ServletContext thisContext = getServletContext();

//-- Assume some method creates a new connection class

Connection newConnection = createConnection();

thisContext.setAttribute( "database.connection", newConnection );

//-- Return some output to the client

} }

Listing 4: retrieving an Object

public class logoffServlet extends HttpServlet{

public void service(HttpServletRequest _req, HttpServletRe- sponse _res) throws ServletException{

ServletContext thisContext = getServletContext();

//-- Assume some method creates a new connection class

Connection newConnection = thisContext.getAttribute( "database.connection");

if ( newConnection == null )

//- Database has not been opened yet

//-- Return some output to the client

} }

Source: http://www2.sys-con.com/itsg/virtualcd/java/archives/0505/williamson2/index.html

Vinay Pareek
  • 111
  • 1
  • 7