Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Story of a Servlet ?

 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I have just started SCWCD preparation without any prior experience of Servlets/JSPs and i am a bit confused. Please read the following and correct me if i have got it wrong:

1-Say, a client posts a GET request. This goes to a web server.
2-Web server forwards the request to a container if request isnt for some static resource.
3-Container finds the correct servlet. It loads the servlet class and creates an object calling its default constructor and then it calls the init() method (which may be defined in servlet class).
4-The init() completes.
5-The container creates a thread. This thread executes the service() method for that servlet.
6-Service() calls servlet's doGet() and servlet's actual work starts.

------------------------------------------------------------------------
-Now suppose another GET request comes for the same servlet.
-Will the container create another servlet object? I think NO and it just starts again from step 5! am i right?


Please correct me.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3-Container finds the correct servlet. It loads the servlet class and creates an object calling its default

It depends. The servlet may already be loaded if the load-on-startup element is set in the deployment descriptor.

Will the container create another servlet object? I think NO and it just starts again from step 5! am i right?

Yes, thus servlets are not thread safe.
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe for the fast reply.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. Only a single servlet object is created, and then called from multiple threads.

As an aside, step 2 is optional. These days, web server and servlet container are often one and the same. E.g., Tomcat -which you'll probably get familiar with before you take the exam- fulfills both these roles.
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gotcha Ulf, thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic