Solved

Servlet related doubt

Posted on 2010-11-17
12
222 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now