aspose file tools*
The moose likes Servlets and the fly likes HttpServletRequest Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpServletRequest Instance" Watch "HttpServletRequest Instance" New topic
Author

HttpServletRequest Instance

Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
Hi Guy's !!!
The servlet init() start's a thread .The code in the run() accesses the database which stores the users session id.All the session id's stored should be checked for validity.How should this be done ... since to check whether a session id is valid or not methods such as lastAccessedTime() need to be used but ... at this time ie. in the run() an instance of HttpSession cannot be obtained because at this time an instance of HttpServletRequest is not available thus ... req.getSession() cannot be used.
The thread performs this operation ... sleeps for 5 minutes ...wakes up ... and performs the same check once again in a cycle.
What should be done ???
Pls suggest.
Thanks Guys .
[This message has been edited by Savio Mascarenhas (edited September 30, 2001).]
[This message has been edited by Savio Mascarenhas (edited September 30, 2001).]
[This message has been edited by Savio Mascarenhas (edited September 30, 2001).]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Whoa! You have some serious misconceptions here.
Servlet init is called ONCE, when the instance of the servlet is created. It is guaranteed to be run before any request is processed. Normally there is only ONE instance of a servlet - it can run for long periods, processing thousands of requests, many simultaneously.
The time to check a user's id is when the session is created, presumably from signin page.
The Thread that processes that request will do fine to check the database, you don't have to create a Thread.
Bill
------------------
author of:
Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
Thanks Bill !!!
Presently workin on a Chat Application.
Bill,the purpose of creating a thread is for "Background Processing" ie. when a servlet is initialized(may be the Login Servlet because it is sure to be initilized) the background processing should begin .
When a user logs into the Chat System his userid,room name & session id are stored in the daatabase.
This "Background Processing" has nothing to do with the current user logging into the system and thus cannot put the code as part doGet()/doPost() which would be called on every request.

The PURPOSE of "Background Processing" is to check whether the session id's stored in the database are valid.These could become invalid in cases such as
a)When the users session is inactive for a period of 15 min.
b)The user has closed the browser ie. without actually logging out of the Chat System.
c)The user has restarted his system.
Thus,the thread needs to check for session validity say every 5 min and if invalid
a)that user needs to be removed from the database also
b)that removal needs to be broadcasted to all the users in the room ie. that particular user needs to be removed from the respective rooms in the front end applet.
Thus,it is in this "Background processing" that an instance of HttpSession is required in order to invoke methods such as lastAccessedTime().
How should it be ???
Thanks a lot Bill !!!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Ok, thats a good usage.
When I do this I create a class as a singleton, where the class extends Thread and has a private constructor. A static getX method creates and starts one the first time it is called, and returns a reference to it when called later.
Be sure to give it a MIN_PRIORITY to minimize interference with requests.
You could also have it implement HttpSessionBindingListener to simplify keeping an eye on sessions.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpServletRequest Instance