This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes object creation order by Servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "object creation order by Servlet." Watch "object creation order by Servlet." New topic
Author

object creation order by Servlet.

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
what is the order of object created by Servlet.

1-ServletContext object

2-Servlet(class) object

3-ServletConfig object

4-Request and Response object

my question is that i read "head first Servlet" where written Request and Response object created before creating Thread.i confused.
please help me.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

What has the thread to do with the objects you listed? Why are you confused?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

first of all the object creation is NOT by servlet. it is by container. as you might have read in head first servlets, a object gains/becomes servlet when its init(ServletConfig config) method completes. before init() an object is just another object. also as the book says that for becoming official card carrying servlet, it needs 2 things which are ServletContext and ServletConfig. so when a web app is deployed, the container first read the DD , creates ServletContext object, reads the context param and make pair of name-value strings and gives the reference of those strings to ServletContext. now the next part. some container initializes all the servlet as soon as the web app is deployed, some other only when a request for that particular servlet comes. i think you can configure this option in containers. whatever the option be, when the request comes, container calls default no-arg constructor of the servlet. then creates ServletConfig object and calls init(ServletConfig) method. so i you are asking about order first ServletContext, then Servlet Object(but it won't be initialized so it won't be real servlet object with all its servletness), then ServletConfig and then the real Servlet object which does all the work
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
gurpeet singh wrote:first of all the object creation is NOT by servlet. it is by container. as you might have read in head first servlets, a object gains/becomes servlet when its init(ServletConfig config) method completes. before init() an object is just another object.
.

what means before init() an object is just another object.please explain.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

That's not an accurate way to put it. The servlet isn't ready to accept requests before its init() lifecycle method is called, but the object doesnt magically change into something else when init() is called.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Ritesh raushan wrote:
gurpeet singh wrote:first of all the object creation is NOT by servlet. it is by container. as you might have read in head first servlets, a object gains/becomes servlet when its init(ServletConfig config) method completes. before init() an object is just another object.
.

what means before init() an object is just another object.please explain.


sorry for creating the confusion. as Bear Sir, has said that until init() method of servlet is called , it is NOT ready to serve your requests. put in other words servlet is not initialized. as servlet has to be initialized before it can call service() method and serve the client/users/request.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

Servlet doesn't create any of those objects. They are all created by the servlet container (Tomcat, Websphere, or whatever).

You need a config to create the context. You need a context to initialize a servlet instance. Request and Response are created by the request processing subsystem which then invokes the servlet service() method (which in turn may invoke doGet, doPost, or whatever).


Customer surveys are for companies who didn't pay proper attention to begin with.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Tim Holloway wrote:Servlet doesn't create any of those objects. They are all created by the servlet container (Tomcat, Websphere, or whatever).

You need a config to create the context. You need a context to initialize a servlet instance. Request and Response are created by the request processing subsystem which then invokes the servlet service() method (which in turn may invoke doGet, doPost, or whatever).



isn't it the other way around i.e. we need context to create a config since context is created at web-module deployment and it is one per webapp per jvm .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object creation order by Servlet.