This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Life Cycle  RSS feed

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that when tomcat server will start, the Servlet will be initialized.
But I want to confirm whether servlet will be initialized each HTTP request comes in & will be destroy after each HTTP response???
How multiple user can access same servlet same time???
Thanks,
Angela
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to find a good tutorial on servlets to fill out the details, but here is a sketch.
1. the servlet engine creates a single instance of your servlet class - either when tomcat starts or when it gets the first request - depending on how you configure it.
2. the same instance handles all requests and may be handling more than one at the "same time" so you MUST not use instance variables for data that changes with each request.
3. the request and response objects handed to your doGet or doPost are unique to a particular user request and are not shared between users.
4. a servlet instance can stay running for days, weeks, months - it is only destroyed when the servlet engine decides to destroy it.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!