Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

STRUTS : diff between JSP implicit session object and HttpSession object

 
Timbu Buddu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in j2ee and struts, if you add an object to the session variable from jsp, can u access it from any java class using HttpSession? Because, I created one as below in x.jsp...
---------------------------------------------------------------------------
<%
if(session.getAttribute("UserPreferences")==null){
UserPreference userPreference=new UserPreference(10,10); session.setAttribute("UserPreferences",userPreference);
}
%>
---------------------------------------------------------------------------
and when I tried to access the session object with name "UserPreferences" from a java class xAction.java as below,
---------------------------------------------------------------------------
HttpSession session=request.getSession();
for (Enumeration e = session.getAttributeNames() ; e.hasMoreElements() {
System.out.println(e.nextElement());
}
UserPreference upf=(UserPreference)session.getAttribute("UserPreferences");
if(upf!=null){
System.out.println("newwwwwwwww");
}
else{
System.out.println("olddddddddddd");
}
---------------------------------------------------------------------------
the output was
oldddddddddddddddd.
It means that xAction class CANNOT access the objects put in session by JSP scriplet.
But when I tried to access the same object (in session variable, with name "UserPreferences"), from another JSP page, I could do that.

So my question is, if u add an object to session variable in a JSP page, can u access it from JSP pages ONLY? Or can u also access it from java classes?

Any help is greatly appreciated.
Thanks,
Srivalli.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
HttpSession session=request.getSession(false);
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Struts forum.
 
Timbu Buddu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.
I got it working when I tested it in a browser, other than the built-in browser of websphere studio site developer.
-Srivalli.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic