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

Struts Action request.getSession returns null

 
Tripp Bishop
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy all,

occasionally it seems that from within our action classes calls to request.getSession() return a null value. Now, if a session hasn't already been established then that's expected behavior. However, we're getting this error after a session has definitely been established. Has anyone seen similar behavior or know why this is happening and how to prevent it?

We're also seeing Servlet exceptions in our JSPs complaining about beans not being found in any scope. The action classes are putting beans into request scope for the pages to use. If the user hits "refresh", magically the bean is found. This is a sporadic error. We know that the name of the attribute is correct in the request object.

We're using Struts 1.1 along with Tiles and Tomcat 4.1.27 embedded in JBoss 3.2.2.

Any help would be greatly appreciated.

Cheers,

Tripp
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getSession() should always return a session because it will create a new one if one does not already exist.

It is possible for request.getSession(false) to return null because the false denotes to not create a new session if one does not already exist.

Your second issue I could see happen if validation failed on a page because there would no longer be a bean in request scope if you return directly to a page.
 
Tripp Bishop
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're just calling request.getSession() and it is most definitely returning null (but only occasionally. The same action object will work fine and handle plenty of requests correctly, then all of a sudden it appears unable to find the session object).

On the second case there is no validation as there's nothing to validate. We're simply taking a DTO, calling request.setAttribute("blah", blah); and forwarding to the correct JSP. The JSP is unable to find bean "blah" in any scope. If you hit refresh, voila, the bean is magically there.

These are definitely sporadic problems. The same JSP finds the bean most of the time. Based on the other problem I'm inclined to thing that it's the request object in the action class that's causing the problems. I believe the JSP when it says it can't find the object.

Does this make it more clear?

Cheers,

Tripp
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be possible that something strange is going on in the server that is killing off sessions. I'm still sticking with saying request.getSession() will never return null. It might be returning a session but the session is getting killed off while you are trying to do something meaningful with it.
request.getSession() might even throw a NullPointerException if the request is getting killed off before calling the method on it.

I think issue #2 is closely related to #1. This might be a server configuration issue. It certainly does not sound like something caused by Struts.
 
Tripp Bishop
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. I wonder if it has anything to do with Tomcat being embedded in JBoss. We never same the request.getSession() = null before we did that. The missing beans is something that we did see before. The question is what configuration setting(s) would even affect this?

Cheers,

Tripp
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic