Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Servlet related doubt

Hi,

I was reading about Initializing a Servlet


The container calls the init method of the Servlet once to initialize it. A ServletException is thrown if initialization fails. But we no longer have to implement this method since the GenericServlet class already has one. If need customization, GenericServlet also has an overloaded init method to further simplify the process – no need to call super.init(ServletConfig config).



I have not understood

>>>>we no longer have to implement this method


also i have not understood
>>>no need to call super.init(ServletConfig config).


Can you please advise. Any ideas, links, resources, sample code highly appreciated. Thansk in advance
0
gudii9
Asked:
gudii9
  • 6
  • 5
3 Solutions
 
HegemonCommented:
>>>>we no longer have to implement this method

javax.servlet.Servlet is an interface. If you were to implement it directly, you would have to implement, among others, the init() method. If you extend the GenericServlet _class_ that already implements the Servlet interface and the init() method, this is not necessary unless you want to.

also i have not understood
>>>no need to call super.init(ServletConfig config).
An implementation overriding this method is required to call super.init(ServletConfig config) - see here: http://download.oracle.com/javaee/5/api/javax/servlet/GenericServlet.html#init(javax.servlet.ServletConfig)

The parameterless init()  method is provided as a convenience. If overridden, it does not require to make the call to super() as above.
0
 
GenroseSusaiCommented:
GenericServlet belongs to javax.servlet package
GenericServlet is an abstract class which extends Object and implements Servlet, ServletConfig and java.io.Serializable interfaces.
Since Implementation is already taken care by GenericServlet, the class extending GenericServlet no longer have to implement this method.



The init() method of servlet can be overridden. Remember the init() method is overloaded in the GenericServlet class. But the difference lies in which init method are u calling..

if you override the init(ServletConfig config) method then you will have to call explicitly super.init(config) inside your code...

if you override the init() method then u dont have to call the super.init() method explicitly..
If in this init method you need the ServletConfig object you can get it via "getServletConfig()"
method. If you define this version of the init method the GenericServlet's
init(config) method will take care of calling your parameter-less init method.


Hope this helps you.
0
 
gudii9Author Commented:
I have not understood what it exactly mean

>>>>The parameterless init()  method is provided as a convenience. If overridden, it does not require to make the call to super() as above.


>>>>If you define this version of the init method the GenericServlet's
init(config) method will take care of calling your parameter-less init method.



can you please advise
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
HegemonCommented:
A method implementing the following method of the Servlet interface:

void init(ServletConfig config);

needs to store the passed config object somewhere.

The GenericServlet implementation does that, therefore in order not to break the logic, if you subclass it and override the above method, your overridden method should call super.init(config);

To free you from this burden, the above GenericServlet's method calls the parameterless init() method which you are encouraged to override instead of overriding the init(ServlerConfig config) method. The implementation looks as follows:

public class GenericServlet() {
...
 
  public void init (ServletConfig config) {
      // store config somewhere;
     ....
     init();
    ....
  }

  public void init() {}; // Blank implementation you override in your class

}

As you can see, the task of storing the config object is already taken care of and you don't need to call super.config().

Hope this is clear.
0
 
gudii9Author Commented:
do ument

>>>>As you can see, the task of storing the config object is already taken care of and you don't need >>>>to call super.config().
you mean super.init(ServletConfig config)  right. please advise

0
 
HegemonCommented:
yes, sorry for the typo
0
 
gudii9Author Commented:
>>>>if you subclass it and override the above method, your overridden method should call super.init(config);


when you say 'if you subclass it'

it means you are referring to GenericServlet right. Please advise
0
 
HegemonCommented:
Yes, I meant creating a class extending GenericServlet
0
 
gudii9Author Commented:
somehow this discussion which is very vital servlet concept seems to be greek and latin to me. I feel like get it at the same time i did not get it. please advise in much more simple words if you can probaby with picture and sample code.
0
 
HegemonCommented:
It looks like you need to refresh your knowledge of OO and inheritance in particular.
0
 
gudii9Author Commented:
Hi,

I think I know OO and inheritance. I did many examples on them. Not sure where i am missing which concept to understand this. Please advise
0
 
gudii9Author Commented:
still not clear on

>>

if you override the init(ServletConfig config) method then you will have to call explicitly super.init(config) inside your code...

if you override the init() method then u dont have to call the super.init() method explicitly..

>>
init

public void init(ServletConfig config)
          throws ServletException
Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet.init(javax.servlet.ServletConfig).
This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).

Specified by:
init in interface Servlet
Parameters:
config - the ServletConfig object that contains configutation information for this servlet
Throws:
ServletException - if an exception occurs that interrupts the servlet's normal operation
See Also:
UnavailableException
init

public void init()
          throws ServletException
A convenience method which can be overridden so that there's no need to call super.init(config).
Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().

Throws:
ServletException - if an exception occurs that interrupts the servlet's normal operation


can you please advise
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now