Solved

Servlet related doubt

Posted on 2010-11-17
12
236 Views
Last Modified: 2012-06-21
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
Comment
Question by:gudii9
  • 6
  • 5
12 Comments
 
LVL 10

Accepted Solution

by:
Hegemon earned 334 total points
ID: 34159351
>>>>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
 
LVL 1

Assisted Solution

by:GenroseSusai
GenroseSusai earned 166 total points
ID: 34167183
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
 
LVL 7

Author Comment

by:gudii9
ID: 34174587
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 10

Expert Comment

by:Hegemon
ID: 34175012
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
 
LVL 7

Author Comment

by:gudii9
ID: 34176530
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
 
LVL 10

Expert Comment

by:Hegemon
ID: 34176897
yes, sorry for the typo
0
 
LVL 7

Author Comment

by:gudii9
ID: 34261094
>>>>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
 
LVL 10

Expert Comment

by:Hegemon
ID: 34262019
Yes, I meant creating a class extending GenericServlet
0
 
LVL 7

Author Comment

by:gudii9
ID: 34284411
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
 
LVL 10

Assisted Solution

by:Hegemon
Hegemon earned 334 total points
ID: 34287345
It looks like you need to refresh your knowledge of OO and inheritance in particular.
0
 
LVL 7

Author Comment

by:gudii9
ID: 34295813
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
 
LVL 7

Author Comment

by:gudii9
ID: 35758242
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 45
swing controls 2 16
web project error add remove 1 31
spring maven example issues 3 12
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question