[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Servlet related doubt

Posted on 2010-11-17
12
Medium Priority
?
259 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 1336 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 664 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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 1336 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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

640 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