• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object creation order by Servlet.

 
Ritesh raushan
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What has the thread to do with the objects you listed? Why are you confused?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic