GeeCON Prague 2014*
The moose likes Servlets and the fly likes detect the session attributes after the session is TimeOut? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "detect the session attributes after the session is TimeOut?" Watch "detect the session attributes after the session is TimeOut?" New topic
Author

detect the session attributes after the session is TimeOut?

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Dear pros and newbs(like me)

I have this session attribute containing an Object.
I also have a session time out set to 30 minutes.
In the servlet code I wrote the code below.

I also forward this object into a JSP page and use it for another purpose. So if I refresh the page when the session is timeout, it will gives me an error too =(

The problem is, when the session is timeout and when I requested the servlet, it gives me an error stating that the object is null or empty.
Does any one knows how to mitigate this problem?
I heard that I must use a listener or a filter. But could anyone here please enlighten me how to solve this problem please.

I really appreciate your help. =)

Thank you Pros!

The full code:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Whenever you fetch a scoped variable from the session using getAttribute() you test it for null. When the session times out, all scoped variables palced in the session are dumped.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Dear Sherif Bear Thanks for the insight.

I have been think for a moment and what I understood is like this



Do all professional Servlet Programmers still do this process? Or do they use another efficient and best practice way to avoid session null error?

Once again thanks for the insight
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

No. You don't test the session itself for null, but for something that you've put within it.

Let's say that when a user logs in we place the user's info in the session as a scoped variable:



Later, when we want to check if the user is still valid or has timed out, we'd fetch the scoped variable, but test it for null:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(One reason testing for session nullity isn't a great idea is that a JSP page will automagically create a session, unless you explicitly configure it not to. It's simply too easy to forget to do that, and not worth the effort. Checking for a session attribute is much simpler, and less error-prone.)
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Ouchhhhhhhh Sir Sheriff Bear, it does not work hmmmmmm. But when I use the previous way "Checking the session null", it does forward the request to another JSP page.

I got this kind of error when I call the servlet again

java.lang.NullPointerException
malo.controller.ProfileController.processRequest(ProfileController.java:41)
malo.controller.ProfileController.doGet(ProfileController.java:134)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're explicitly telling it not to create a session if there isn't one--what else would you expect?
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
@ Mr.Newton, thanks for the insight

So it is best practice or better to check the session attribute nullity rather than checking the session object nullity.

so if the session attribute is destroyed/expired/timeout, I must create a new session attribute filled with an object and a value such as a boolean which tells the visitor is not a member. Then in a JSP page it will use that boolean information to hide the special feature and display the login/signup button.

hehehehehe sorry I made too much statement instead of an question.

The question: Is my statement correct?

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
@Mr Newton

ouchhhhhh let me fix that, and I will be back soon. 5 minutes =)
Thanks
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
@ EVERY ONE

Oh my GAWTTTTT it's Workinggggg


hehehehehe, thank you Pros! You have made my day! =D

God Bless you all
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Fixed Code. Thanks to Mr.Bear and Mr.Newton

 
GeeCON Prague 2014
 
subject: detect the session attributes after the session is TimeOut?