• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpServletRequest Instance

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic