• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Basic Question.

 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In the servlet life cycle, the servlet is first loaded and then instantiated, then the container class the init() method.
i.e.
Load Servlet Class
Instantiate Servlet, i.e. call the constructor.
Initialise the servlet i.e. call the init().
Why do we need to have init() if the servlet life cycle has Contructor calling.
Why didn't the Sun use contructor instead of introducing init() method. ?
TIA
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Actually if you allow to write the construtor then programmer might write parametric construtor which can not be dynamically loaded by servlet engine.
To avoid this confusion there is init().
Correct Me If I am Wrong
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Since Servlets is a specification given by the Sun, they can ask the programer to use the contructor properly. I mean make it madatory to declare default contructor for container purpose and customs ones for programmer use.
Isn't it?
Why new method init() ??
 
Ramesh R G V S
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
As you said Servlets is a specification given by the Sun, they can make it madatory to declare default contructor for container purpose and customs ones for programmer use.
Each servlet is part of default context and u need pass this as an argument to access info about other servlets.Now sun either should make sure that these are getting passed properly if they would have allowed container to use constructor or provide a standard method.Sun would have preferred to provide standard method.
Regards
Ramesh
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I agree that Sun has introduced the init() to make sure the context of the servlet is properly handled by the container.
But why there must be some good reason for doing it right. Since the constructor of a class is always called first when the class is instantiated.
Any Sun guys out here.. or guys preparing for architect exam..
Cheers.
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi L Goundalkar,
We all have noticed the case in an Applet's life cycle that init() is called first and only once as like as servlet's init() is. The reason is historical....at the early java ages, constructors were not permitted to be overloaded. So if programmers needed to conveniently use the initial configuration of a servlet or applet object, they were given the provision of init(). Though after that, constructor overloading was allowed but init() was already a string practice all over the industry which SUN might have considered not to disturb. You will a few information on this from Jason Hunter's classic book "Java Servlet Programming".
Are you preparing for SCEA OR scwcd EXAM?
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ashik,
Thanks for the info.
So that means there is no specific technical reason for using redundant init() in servlets.
BTW I am preparing for SCWCD.
Cheers.
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
The reason for having a separate init() method is a matter of design. The best way to create a system comprising several sub-components is by creating a set of interfaces and forcing the implementations of the sub-components to stick to the interfaces.
The init(ServletConfig); method is in the javax.servlet.Servlet interface. By way of interfaces, the API forces the developers to implement the correct methods even before they can compile their servlet classes. If they had taken the approach of constructors with ServletConfig as an argument, then non-compliant classes can be caught only at load-time.
What we usually do is extend the HttpServletClass which extends GenericServlet. To make our lives easier, GenericServlet implements init(ServletConfig); for us and provides us with a convinient init(); method that we can override only if we need to do some initialization.
Second reason is that instantiation and initialization are two different things. The servlet container may (for some reason) choose to create an instance of the servlet but not call init(ServletConfig) for some time.
HTH
-j
 
Shashank Tanksali
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet container does not care if your class has a constructor or not. All it cares about is whether your class is an instance of GenericServlet.
If a constructor were used to initialize the servlet, then if the container tried to initialize the servlet by invoking the servet's one argument constructor, then it could throw a "No such constructor/method error" at runtime if the servlet did not have the appropriate constructor.
However if it is an instanceof GenericServlet, then there is a default implementation of init in GenericServlet, which does nothing. The container would not have any problem initializing the servlet in that case.
Also the constructor can be used by the servlet for something else.
Since init() throws ServletException, declaring an empty constructor and throws ServletException would not even compile.
Also remember that init() is also used by the GenericServlet to initialize stuff. If a constructor were used, it would be the responsibility of the developer to invoke super() if the constructor were empty.
Just my .02
Regards,
Shashank
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic