I'm setting a bean with some data in a JSP page, but then in my servlet when I try to access the value, I get a null value back. I printed the session IDs in both pages and I'm getting different values. Does anyone know what might be happening? Thanks.
My best guess is that your session is getting invalidated somewhere. If you do have session.invalidate()
statements somewhere in your code, put breakpoints there and verify in debug mode if any of them is being hit.
You can also implement and set up a HttpSessionListener
in your project. Implement the callback interface and add the implementation class to your web.xml
:
<listener>
<listener-class>com.xyz.AppSessionListener</listener-class>
</listener>
Put a breakpoint within sessionDestroyed
method and see when it gets hit in debug mode.
I didn't notice that you opened a new question for this. This issue has been answered in the comments of your previous question. Here's an extract of relevance:
Sessions are domain and context dependent. If the servlet listens on an URL of a different domain and/or context than the JSP, then it will indeed use a different session. You can however let different contexts share the same session by configuring the servletcontainer accordingly (in Tomcat, set
emptySessionPath
attribute of<Connector>
totrue
). But you cannot share sessions among different domains due to security restrictions in HTTP spec.
The other cause is indeed that you explicitly invalidated the session by HttpSession#invalidate()
, but that would have been too obvious to spot in your code.