views:

195

answers:

1

I am writing a web application that uses a JSP usebean tag in the session scope as shown below

<jsp:useBean id="userSession" class="project.session.UserSession" scope="session" />

I have also written a filter which does some processing and needs to set some values on the userSession bean. How do I get a handle onto the object and set values on it? I have tried getting the object from session as shown below, but this method doesn't work.

UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");

I use Tomcat for development.

+1  A: 

If it is null (it will always be on the first request), then you just need to precreate it yourself.

UserSession userSession = (UserSession) request.getSession().getAttribute("userSession");
if (userSession == null) {
    userSession = new UserSession();
    request.getSession().setAttribute("userSession", userSession);
}
userSession.doSomething();
BalusC
A small change to the setAttribute though. I was able to get this working by removing the attribute from session first and then adding the newly created session attribute.
Ritesh M Nayak

related questions