Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Redirection after session Timeout. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Redirection after session Timeout." Watch "Redirection after session Timeout." New topic
Author

Redirection after session Timeout.

Amit Punjwani
Ranch Hand

Joined: Jul 10, 2000
Posts: 50
I am working on a project on JSP. I am actually working on a normal catalogue display site wherein i am gettin the details of the products from the database and putting them in sessions.

My problem is when the session expires after 30 minutes i.e. in Tomcat, I want the user to be redirected to the main page of the site since the session had expired. At present I have an error page which just displays the message of the error . So it displays null. But the user wouldn't understand that. So I just want to know what do i need to do inorder to redirect the user to the main page of the site instead of displaying him the errorpage.
Thanx in advance,
Amit
rkjhaver
Greenhorn

Joined: Jan 08, 2001
Posts: 4
whenever user refresh the page after 30Mins.
the session is expired it will give some by default which user cant understand.
before loading the page u check the session value.whether the value is null or not. if it is null redirect the page which will give user define message saying than u r no longer user....
please relogin again..
hope it will work fine.
rajesh kumar jhaver
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"rkjhaver",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
try checking for the Session-I value before every user request. I f null is returned then redirect the user to your page of wish...
hope this helps
sagar
Amit Punjwani
Ranch Hand

Joined: Jul 10, 2000
Posts: 50
Hi guys,
thanx for u're replies.
but see i am using beans with session scope to store my data at the session .
so is it ok if I do somethin like this
<code>
<jsp:useBean id="Perfumes" class="Perfume.Perf" scope="session"/>
<%
if(Perfumes == null) {
response.sendRedirect("http://localhost:8080/home.html");
}
%>
thanx in adv
amit
------------------
Karn Sadyapongse
Greenhorn

Joined: Jan 09, 2006
Posts: 13
why it doesn't work for my project ?
it's still show tomcat errors

<code>
<jsp:useBean id="Perfumes" class="Perfume.Perf" scope="session"/>

<%

if(Perfumes == null) {
response.sendRedirect("http://localhost:8080/home.html");
}
%>



Is it the right way to do like this,to sendRedirect when session was time out ?
i need help
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
it's still show tomcat errors

How can we possibly answer this when you dont say what error you got?

Bill
Karn Sadyapongse
Greenhorn

Joined: Jan 09, 2006
Posts: 13
i'm sorry ,
the error is about..NullPointerException when doGet() or doPost() in servlet try to call the bean (which is already expired)
in my case,i fixed by
insert try{
}catch(Exception e){
}
cover all processes of servlet
(when NullPointerException occured,it will be throw to catch Exception
,in catch,i put sendRedirect to first page of webapp)

is there another way to fix this problem ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Yes, find the code causing the null pointer exception and fix it!

Eating an exception to "make it go away" is never the right solution.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Karn Sadyapongse
Greenhorn

Joined: Jan 09, 2006
Posts: 13
NullPointerException occured because the session that servlet called was already expired,
how can i check that if the session was expired,don't call them

can i ?
Sathiq Ali
Greenhorn

Joined: Jan 26, 2006
Posts: 9
Hi
Check out this Link
http://www.atg.com/repositories/ContentCatalogRepository_en/manuals/ATG6.0.0/apidoc/atg/projects/b2bstore/servlet/CheckSessionExpiration.html

There is a class Check Session Expiration. By using this class we can redirect to Home Page. Try it out
wish u all the best
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

An expired session should not result in a null pointer exception. I think you need to dig a little deeper.
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Why don't you use Listeners?
Shrinivas
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Testing for expired sessions by testing the session object itself for nullness isn't always the most reliable way to go.
This is especially true if you're using a useBean tag like this:
<jsp:useBean id="Perfumes" class="Perfume.Perf" scope="session"/>
The useBean tag will create a new session if the previous one has expired.
So, your test for an exsiting session will return true, even though it isn't the same session that the user had when they originally logged in.

I've had much better luck testing for the existence of an object bound to session. When the user logs in sucessfully, create an object ("userBean") and bind it to session. Then with each hit, check to see if that object is null (filters are nice for this).
If it is, forward or redirect to the login page.



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Side note:


Compilers don't care about coding conventions and will happily work with your code no matter what capitalization scheme you follow. Humans, on the other hand, will find it very hard to read your code if you don't follow them.

An object variable (perfumes) should start with a lower case letter.
The class's name should start with an upper case "Perfumes".
Package names should be all lower case letters.

This may sound like nitpicking but seeing this:
<jsp:useBean id="Perfumes" class="Perfume.Perf" scope="session"/>
makes me wonder if the problem your having isn't due to invalid values in the 'id' and 'class' attributes in the tag.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Redirection after session Timeout.
 
Similar Threads
General JSF page navigation questions
problem with session and frames
handling session time out
servlet/jsp question
User Session Management