File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes STRUTS : diff between JSP implicit session object and HttpSession object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "STRUTS : diff between JSP implicit session object and HttpSession object" Watch "STRUTS : diff between JSP implicit session object and HttpSession object" New topic
Author

STRUTS : diff between JSP implicit session object and HttpSession object

Timbu Buddu
Greenhorn

Joined: Apr 01, 2003
Posts: 8
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

Joined: Feb 05, 2003
Posts: 4727

Try:
HttpSession session=request.getSession(false);


A good workman is known by his tools.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61101
    
  66

Moving to the Struts forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Timbu Buddu
Greenhorn

Joined: Apr 01, 2003
Posts: 8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: STRUTS : diff between JSP implicit session object and HttpSession object