Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session var set in JSP

 
sean cee
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have set a session, application var. in a JSP as follows:
<%
..
session.setAttribute("debugmode",String.valueOf(debugmode));
getServletContext().setAttribute("log",log);
..
%>
then this JSP will call a Servlet and from that Servlet I am try to use those session, application var. as follows:

HttpSession session = request.getSession(false);
Boolean tmpb = new Boolean((String)session.getAttribute("debugmode"));
BufferedWriter log = (BufferedWriter)getServletContext().getAttribute("log")

Which is nothing complicated but both values are null when I try to retriev them from the Servlet.
Can someone shed some light on this?
Thanks
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like a silly thing to ask, but both 'debugmode' and 'log' are variables that are in scope when you place them into the session and application contexts in your JSP?

 
sean cee
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.
and within that JSP I can look up those attributes as well.
 
Ganga Ch
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpSession session = request.getSession(false);
Boolean tmpb = new Boolean((String)session.getAttribute("debugmode"));
BufferedWriter log = (BufferedWriter)getServletContext().getAttribute("log")
Hi, you might have found the solution, but dont you have to set the session to "true" when you "getSession".
-G
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe -- maybe not. The difference between getSession(true) and getSession(false) is that getSession(false) will return null instead of an HttpSession object if a session has not yet been established (with getSession(true)) for that user...
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic