tags:

views:

32

answers:

2

Hi,

I have a jsp page which has different IF conditions.On load of the jsp page,one of the if condition will be invoked based on value set to fuse action. I have a problem like, first time when the page is loaded then IF condition1(say, for ex) is invoked and a variable is assigned to the HTTPCOntext session object. Second time when the page is loaded(meaning after form submission), another IF condition2(Say for ex) is invoked.I tried accessing the session object in this if condition, the value in the session object is null.

Is it something like session variable in one scriptlet is not accessible by other scriptlets in the same jsp? I need to access the session object in second IF condition, please let me know the way to acheive this.

Thanks in Advance Rupa

+1  A: 

Several facts:

  • Sessions are backed by cookies.
  • Cookies are by default bound to a specific domain and context path.
  • When cookies are disabled in the client side, you have to take care over URL rewriting to include the session ID as jsessionid.
  • Writing raw Java code in a JSP file is a bad idea.

So if you've stored an object in the session and it is not available in the next request, then it can have the following causes:

  • The other JSP page is in a different domain or context.
  • The client has cookies disabled and you didn't take care about URL rewriting.
  • You are storing and accessing it the wrong way.

Since you're talking about something like "HTTPCOntext" which makes no sense in terms of JSP/Servlet and are using a single JSP page, I think it's the last one of the possible causes. Here's a kickoff example of how to store an attribute in the session the proper way:

Object someObject = "This is just a String.";
request.getSession().setAttribute("someName", someObject);

and here how to obtain it:

Object someObject = request.getSession().getAttribute("someName");
BalusC
A: 

session.putValue is deprecated

Use session.setAttribute(String name, Object value) like this session.setAttribute("Test",testStringVariable)

and fetch it by session.getAttribute("Test"). This should work.

Have you confirmed that session.getAttribute("Test") is itself giving null, or have you initialized some variable to null and printing out that one instead.

JoseK

related questions