This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
If I studied well, these objects are created by the server container everytime a client sends a request. then if the servlet has already been initialised, the container creates the two objects HttpRequest and HttpResponse along with a specific thread (one per request) and then passes their reference to the servlet that corresponds to the client-requested URL.
hope I've been clear.
Thanks Fio..this has helped me correct my basic understanding ..
However, I would rather want a little detailed explanation through some sample code, like what actually happens when a user clicks on Go button in a browser, after the URL is decoded into host name, port no, specific file requested for. Also wanted to know what happens when servlet initialization is for the first time...
Joined: Jul 22, 2010
this may help you understand better.
Head First JSP and servlets serch for page 42, there's an useful diagram which shows you the lifecycle of request and responses.
I'm studying on this book and I recommend it to you if you want to learn and keep in mind how servlets actually work.
Naveen Kumar Kumar
Joined: Jul 28, 2010
okay thanks....let me check it...
Joined: Jul 22, 2010
sorry I seen only now your last post, I'm not such an expert with code
but, as long as I know:
Before intialisation, a servlet is just a plain java object. then the container calls the method init() on it and it becomes a servlet, that means that it can handle client requests via the Service() method. the method init() is dependent from ServletConfig and ServletContext, two sets of information that can be set in a xml file called DD, deployment descriptor.
ServletConfig is a set of infos only for a specific servlet, ServletContext is visible from all the app (as long it's not distributed), and they are read only once in the lifecycle of a servlet. if you change some values you'll have to redeploy the servlet.
Client-side the user types an url, usually basing on the HTTP protocol. the well-known port for HTTP is 80 from the client. (there's a lot of other stuff regarding socket initialisation and port allocation by server side in between I've learned in a previous long-time ago exam I'm not sure I could explain correctly). then http unwraps the HHTP request from all the lower level protocols encapsulation and retrieves the methods (Post ,Head, Get, whatever) and the URL requested. when a server receives a new Http request, it searches for the url in the dd.
in the dd there are tags which map the user-known name (contained into the url) to the middle name and other tags that map the middle name to the real pathname of the requested document as it's on the server, so you can set a correspondance.