Solved

Servlet related doubt

Posted on 2010-11-17
12
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert from xls to xlsx using java 7 159
Adding multiple JVM environments to RedHat 6 7 81
Is there a simpler dropbox system? 10 53
Session in java desktop 5 37
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

739 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