*
The moose likes Servlets and the fly likes when and how all methods of servlet are called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "when and how all methods of servlet are called" Watch "when and how all methods of servlet are called" New topic
Author

when and how all methods of servlet are called

A Chavda
Greenhorn

Joined: Apr 15, 2008
Posts: 3
Hi All,

I know the lifecycle of the servlet. But i am get confuse with the invokation of each method when there is a request. Say you have Jsp for the view part and servlet. Please let me know the flow of the request right when the button in the browser is clicked to new page is displayed. Please correct me If I am wrong.

1. When the button in the page is clicked, a request is generated. First JSP is called and all the form data are stored in the form bean. Then the servlet is called. How we know which servlet will be called? Also do we have different servlet called for different request?
2.Say the servlet is called. Web Container will load the servlet and calls the init method passing the configuration information to the container and thus the container intialize[make the instance of] the servlet.
3.Then service method is called which will process the request which means validate form data. Is the service method called for each request? I mean if two request are there service method of the same servlet is called twice?
4.When the container wants to destroy servlet instance it calls destroy method. Do we have to invoke specially the destroy()? What are the circumstances when container wants to destroy the servlet? Can we call destroy() method from service()? If no what will happen if we call?

I know the question is too big. But i will appreciate if someone clear this concept.

Thanks in advance
A Chavda
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61421
    
  67

Originally posted by A Chavda:
1. When the button in the page is clicked, a request is generated. First JSP is called and all the form data are stored in the form bean. Then the servlet is called. How we know which servlet will be called? Also do we have different servlet called for different request?
What happens entirely depends upon what the URL is that generates the request. Without this information, no one has anyway of knowing what will happen. No JSP is automatically called. No "form bean" (whatever that is) will automatically be created. And no servlet will automatically be invoked. What if the URL addresses an image file? An HTML page? A JavaScript file? It all depends upon the URL.

2.Say the servlet is called. Web Container will load the servlet and calls the init method passing the configuration information to the container and thus the container intialize[make the instance of] the servlet.
Not necessarily. The servlet will be loaded once and its init() method called once. It could happen on the first request, or it could have happened before the first request. It will not happen again after the first time.

3.Then service method is called which will process the request which means validate form data.
Form data is not automatically validated. The servlet will perform whatever action its code causes to happen.

Is the service method called for each request? I mean if two request are there service method of the same servlet is called twice?
Yes.

4.When the container wants to destroy servlet instance it calls destroy method. Do we have to invoke specially the destroy()?
No, the container handles it.

What are the circumstances when container wants to destroy the servlet?
Usually when the web app is being unloaded.

Can we call destroy() method from service()? If no what will happen if we call?
Yes, and it will just execute whatever code is in the method. It will not cause the servlet to be destroyed. It would be a very silly thing to do.
[ September 20, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: when and how all methods of servlet are called