File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How to implement timeout functionaily using JSP and Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to implement timeout functionaily using JSP and Servlets" Watch "How to implement timeout functionaily using JSP and Servlets" New topic

How to implement timeout functionaily using JSP and Servlets

Sreek Gupta

Joined: Mar 08, 2006
Posts: 24
I have the following scenario. But i am not getting any idea how to implement it. please help me . I would be very thankful if anyone can send code samples.

Following is my problem.

I will be having some users who can access my web site. To access my web site they have to pay some money . Based on the money that users paid to me i will allocate a time limit to them (and it will be stoed in DB)to access my web site. Once they exceeds the time limit they should get a alert message saying that your time is over and please recharge your account.

I mean , Suppose a user paid 100Rs to me. Then say i allocated 100minutes to him. So he can access my site for 100 minutes. Once he crosses 100 min i should not allow the user to access my site any more . And i have to display an alert message to the user saying that his time is exceeded.

Also if he logs in and logs out in 20 min (say) , then i have to update the time limit in DB to 80 minutes.

I have to implement this using java,jsp,java script and servlets.
So please help me out if you have any ideas.

Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Originally posted by sreek ss:
I have to implement this using java,jsp,java script and servlets.
So please help me out if you have any ideas.

Can you user Applet?

My blood is tested +ve for Java.
Sreek Gupta

Joined: Mar 08, 2006
Posts: 24
Hi ,
I do not have any applets in my application.
Is it not possible using JSP , Servlets ?
If not possible , Can you please advise me how can i do this using applets.
As i am new to these technologies , please help me with code samples.

Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Well without Applet also it is achievable.

Tell me the business requirements so that I can suggest according to that.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
It should be better done on server-side. You can always apply some logic.

Well, i am listing down some steps

- Get the time alloted for that particular user from DB while authenticating user.
- Record the current time just after a successful authentication and call it login time
- Take difference between the current and login time upon each request and compare that with the time alloted
- invalidate the session accordingly and redirect the user to some message page

Are you getting me??
Sreek Gupta

Joined: Mar 08, 2006
Posts: 24
Following are my business requirements.

1. Initially users pay money to me to access my website. Then i will create a entry in Database for that user. Based on the money he paid to me i will allocate duration to him. Typically my database table looks as follows.


Upon registration administrator populates this table.

2. After payment, he is eligible to access my website. He can logon to my site by entering username and password which has been created at login time.

3. Take the scenario now. Suppose user 'A' has paid 100Rs to me. Then i will register him into the system. Username, Password, Amount, Duration will be populated in the database.
Now say following is the entry present in Database.

DURATION 6000000 milli seconds i,e (60 minutes)

4. Now user 'A' can log into the system by entering username and password. If he logout the system in 1000000 milli seconds then database has to be updated accordingly. That means DURATION field in the database has to be updated to 5900000 milli seconds. He can come next time and use the remaining time.

5. When user is left with 3 minutes , then he has to be displayed with a alert saying that only 3 minutes left and to recharge again.

6. If he continue to use my website more than 60 minutes without recharging , then he has to be disconnected and should not be allowed to login again until he recharges again.

I am not getting how to proceed. Do we need to access the database indefinitely to get the duration every second? What could be the best way in these kind of scenarios.

Please help me with code samples. Also please let me know if there is any confusion in the requirements.

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

I would look at the HTTP Session Listener:

There are also some methods in HTTP Session for determining how long a session has been active:

Remember, HTTP is a stateless environment.
This means that you have no way of knowing if someone's browser (or internet connection) has crashed or if they wandered to another site without logging out. If you are charging them by the minute, you will probably want set a real short session-timeout value.
Normally this would be a source of irritation to the user but compared with being charged for time that they're not using, having to log in after 5 minutes of inactivity probably isn't so bad. With a little Javascript, you could write a poller that hits the site every n seconds to keep the session active as long as they have the browser pointed to your site.

I wrote a small demo app that monitors user sessions.
Some of that code might be interesting to you.
Look for SessionMonitor.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63060

"sreek ss",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

JavaRanch Sheriff

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I haven't gone through the entire thread and here are my suggestions

Check getCreationTime() method of HttpSession. With this method you can limit a user to say 10 minutes before the session expires. I haven't implemented this.

There are lot of free code in javascript with which you can easily do this. But I am not sure whether this would be an appropriate solution.

Javascript Solution
Time limit on viewing document script

keep us informed about your progress.
[ March 09, 2006: Message edited by: Vishnu Prakash ]

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
I agree. Here's the link:
subject: How to implement timeout functionaily using JSP and Servlets
It's not a secret anymore!