Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem getting session attributes  RSS feed

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use Tomcat 4.1.24 on a Solaris system as my Servlet, JSP container.
It's strange that within the same web application, some of my JSP pages can get the session attributes I set, but some cannot.
Say, in a servlet, I did
HttpSession session = request.getSession(true);
String userEmail = request.getParameter("email");
session.setAttribute("email", userEmail);
This servlet will forward to page1.jsp, from which I can get the email attribute through session.getAttribute("email");
page1.jsp has link to page2.jsp, from which, I *cannot* get the email attribute through session.getAttribute("email"); However, I did
System.out.println(session) in page2.jsp
And session is *not* null.
And when I do the following in page2.jsp,
java.util.Enumeration enum = session.getAttributeNames();
System.out.println("Total session attributes");
while(enum.hasMoreElements())
{ System.out.println(enum.nextElement());
}
nothing gets printed out from the while loop.
Do you guys understand what problem this is? I don't understand. Please kindly tell me. Thanks a lot.
 
Jason Taylor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a guess and Im no expert but dont you need to typecast the object you inserted into the session back to what it was when you put it in.
Then it looks like you need to use the object getmethods to retrieve the info you desire.
ie
Someobject temp = (Someobject)session.getAttribute("name");
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are all of the JSP pages in the same "web application"? If not the session in one won't be visible in the other and they are separate sessions. Try printing out the session ID to see.
Bill
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason, you are right, I missed the cast, but that does not help resolve the problem.
William, yes, they are in the same web applicaton.
Look at the printout from page1.jsp and page2.jsp after I added the typecast:
In page1.jsp:
Session: org.apache.catalina.session.StandardSessionFacade@b914b3
Total session attributes in page1.jsp
email
str
In page1.jsp, email: somebody@somewhere.com
In page1.jsp, str: just a test
In page2.jsp:
Session: org.apache.catalina.session.StandardSessionFacade@fdfc58
Total session attributes in page2.jsp
In page2.jsp, email: null
In page2.jsp, astr: null
The session IDs do look different, but I am not sure if this is a problem and why this happened.
[ May 25, 2003: Message edited by: Gene Chao ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are certainly seeing two different session objects. (Incidently, the HttpSession method getID() will return the actual id used in the cookie by the browser).
Come to think of it - are you sure the browser you are testing with is allowing cookies?
Bill
 
kavitha rama
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure that your jsp has
<%@ page language="java" contentType="text/html" session="true" %>
and session is not timing out
 
Wendal Park
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I think the problem could be that those references in the session attributes are really NULL.
For example, if I have:
String example1 = null;
// Do something
session.setAttribute("Example1", example1);
And if that "Do Something" part does not assign a value to example1, then when you go:
session.getAttribute("Example1");
It would return NULL, and that makes you think your "Example1" reference was misteriously gone in your session attribute. But really it's just storing the NULL reference.
Therefore, make sure you really have something to put into session before calling "session.setAttribute(..., ...)". Just FYI, one good practice to remove an attribute is to go "session.removeAttribute("Example1")" and NOT "session.setAttribute("Example1", null)". Then your list of attributes in session (depending on different app servers) will not expand that quickly.
 
Rohit Ahuja
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chao,
Can u paste the important parts of ur code right here??
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should paste your latest code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!