• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception with session attribute.  RSS feed

 
Jojo Plena
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to maintain the request count for each individual client. It works well with one client, but I get exception (in the second statement) when second client sends the request.

Thanks in advance for any help.



[BSouther: Added UBB CODE tags]
[ August 24, 2007: Message edited by: Ben Souther ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi soso ,

Please check with the Javaranch's naming policy.
http://www.javaranch.com/name.jsp

What exception are you getting ?
[ August 24, 2007: Message edited by: Rahul Bhattacharjee ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exception, what is the message, where is it thrown?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In apps that are a mixture of servlets and JSPs, there is a good chance that the session will not be new when your servlet gets to it. (JSPs by default, create a session).

One possible problem I could see with this (but we will never know until you answer David's question) is..
If the session already exists when this servlet is hit, then the code block above will never be run. You would then get a NullPointerException on the line that tries to increment the Integer.

Two approaches that might be more reliable.
1.) Instead of testing the session with isNew, test the 'requestCount' attribute for null.
If it's null, create it, set it to zero and bind it to session scope.

2.) Use a sessionListener to populate the requestCount attribute.
 
Jojo Plena
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:


In apps that are a mixture of servlets and JSPs, there is a good chance that the session will not be new when your servlet gets to it. (JSPs by default, create a session).

One possible problem I could see with this (but we will never know until you answer David's question) is..
If the session already exists when this servlet is hit, then the code block above will never be run. You would then get a NullPointerException on the line that tries to increment the Integer.

Two approaches that might be more reliable.
1.) Instead of testing the session with isNew, test the 'requestCount' attribute for null.
If it's null, create it, set it to zero and bind it to session scope.

2.) Use a sessionListener to populate the requestCount attribute.



I get a NullPointerException in exactly the same line that you have indicated. But I disagree your arguement. And here it is why.

When a new client requests for this particular servlet, a new session is created. The (session.isNew()) is true and hence
session.setAttribute("RequestCount", new Integer(0)) statement gets executed. If request come from the same client(in the same session), there's no necessity of setting the "RequestCount" attribute, because it already exists. So, there should be no NullPointerException.
What do you say?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by soso dodo:
What do you say?


I'd say that your assumptions are not sound and that you should pay attention to the suggestion that using isNew() for this sort of thing is fraught with pitfalls and issues.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by soso dodo:



I get a NullPointerException in exactly the same line that you have indicated. But I disagree your arguement. And here it is why.

When a new client requests for this particular servlet, a new session is created. The (session.isNew()) is true and hence
session.setAttribute("RequestCount", new Integer(0)) statement gets executed. If request come from the same client(in the same session), there's no necessity of setting the "RequestCount" attribute, because it already exists. So, there should be no NullPointerException.
What do you say?


I understood your logic when I first read your code.
The problem is that your logic relies on THIS servlet being the absolute first part of your app the the user hits. If the user brushes up against any other part of your app (remember all JSPs will create a session unless you specifically disable session handling in each and every one) before getting to that servlet, isSessionNew will return false and you code will throw a null pointer exception when it tries to work with the session scoped Integer.

Either of the two alternatives that I've suggested will make your code more robust.

Have you considered what would happen if someone bookmarks one of your JSPs causing them to hit a JSP before going to your servlet?
 
Jojo Plena
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:


I understood your logic when I first read your code.
The problem is that your logic relies on THIS servlet being the absolute first part of your app the the user hits. If the user brushes up against any other part of your app (remember all JSPs will create a session unless you specifically disable session handling in each and every one) before getting to that servlet, isSessionNew will return false and you code will throw a null pointer exception when it tries to work with the session scoped Integer.

Either of the two alternatives that I've suggested will make your code more robust.

Have you considered what would happen if someone bookmarks one of your JSPs causing them to hit a JSP before going to your servlet?



Yeah, I got your point. I appreciate. Thanks again.
[ August 25, 2007: Message edited by: Jojo Plena ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!