This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes HttpServlet Request and Response objects creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpServlet Request and Response objects creation" Watch "HttpServlet Request and Response objects creation" New topic

HttpServlet Request and Response objects creation

Naveen Kumar Kumar

Joined: Jul 28, 2010
Posts: 19
hey there!!!

my first query on java ranch...this is absolutely a great forum..and wish i will be having a great learning time..

Q: My query is , I wanted to know how, when and where the HttpServletRequest and HttpServletResponse objects are created and sent as input arguments to the doGet() method in a servlet.

PS: Please forgive me if this has been already answered. Would be grateful if you can share me the link for that thread.

Thanks a lot in advance...


Push the limits to push yourself..!!
Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38

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.

[I study here]
Regards, FiĆ²
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60800

"practicalhead bean", please check your private messages for an important administrative matter. Thanks.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Naveen Kumar Kumar

Joined: Jul 28, 2010
Posts: 19
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...

Thanks again...cheers....
Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
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
Posts: 19
okay thanks....let me check it...

Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
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.

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.

This is all I know about that.
I agree. Here's the link:
subject: HttpServlet Request and Response objects creation
Similar Threads
validation problem
JAXB persistency
Hibernate Criteria Query not working properly
n-ary association
Jobs in US