File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Basic Servlet Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Basic Servlet Doubt" Watch "Basic Servlet Doubt" New topic
Author

Basic Servlet Doubt

Rakesh Maddala
Greenhorn

Joined: Aug 05, 2010
Posts: 10
Hi All,

I am new to Java and J2EE.

I have a class "MyServlet extends HTTPServlet". When i call super.init() method from MyServlet, why should it call init method of the GenericServlet?

I would really appreciate the help.

Thanks


Warm Regards
Rakesh Maddala
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Welcome to JavaRanch!

Didi you check the API of HttpServlet and GenericServlet? Do HttpServlet have init() method?

The purpose of the init() method is to initialize the servlet. Since HttpServlet is an abstract class, so we can't instantiate it alone. So no need to initialize it(no instantiation -> no initialization), that's why, init() isn't there.

[EDITED] Some mistakes are corrected!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rakesh Maddala
Greenhorn

Joined: Aug 05, 2010
Posts: 10
Thank you,

I know HTTPServlet is an interface. when i say "MyServlet extends HTTPServlet" the super class is HTTPServlet. Where does GenericServlet comes into picture?

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

GenericServlet is used if you want to implement non http service.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Rakesh Maddala wrote:Thank you,

I know HTTPServlet is an interface. when i say "MyServlet extends HTTPServlet" the super class is HTTPServlet. Where does GenericServlet comes into picture?



No, HttpServlet is an abstract class, not a interface. Did you check the hierarchy of user servlet?

MyServlet extends HttpServlet.
HttpServlet extends GenericServlet.
GenericServlet implements Servlet.

This applies if your servlet(MyServlet) is supposed to be used for HTTP usages!
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Rakesh Maddala wrote:I know HTTPServlet is an interface. when i say "MyServlet extends HTTPServlet" the super class is HTTPServlet. Where does GenericServlet comes into picture?

HttpServlet is an abstract class, not an interface, and it extends GenericServlet. All this can be found in the javadocs. Since HttpServlet does not override init, it is GenericServlet's init method that gets called.

Harpreet Singh janda wrote: GenericServlet is used if you want to implement non http service.

No, GenericServlet is always used (note that HttpServlet extends GenericServlet). All that is needed for a servlet is to implement the Servlet interface, though - using HttpServlet is simply more convenient than to implement all the methods yourself.
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Lester Burnham wrote:
Harpreet Singh janda wrote: GenericServlet is used if you want to implement non http service.

No, GenericServlet is always used (note that HttpServlet extends GenericServlet).

I think you have misunderstood what Harpreet said. I think he is trying to say is, if the servlet wants to serve the http requests then it need to extend the HttpServlet. If it needs to serve the non-http request like 'ftp' then it need to extend the GenericServlet. Because HttpServlets are capable of serving only http request.
Lester Burnham wrote:All that is needed for a servlet is to implement the Servlet interface, though - using HttpServlet is simply more convenient than to implement all the methods yourself.

Yes. Again in case of 'ftp' requests, I can achieve the same by extending the GenericServlet, since it implements Servlet Interface.


A.A.Anbarasu
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Anbarasu Aladiyan wrote:I think you have misunderstood what Harpreet said. I think he is trying to say is, if the servlet wants to serve the http requests then it need to extend the HttpServlet. If it needs to serve the non-http request like 'ftp' then it need to extend the GenericServlet. Because HttpServlets are capable of serving only http request.

While we could argue all day about what some other person meant by their words, the sentence to which I responded is incorrect as it stands, and that's what I addressed. As I also mentioned, it is not necessary to extend either GenericServlet or HttpServlet, no matter what protocol is used - implementing the Servlet interface is sufficient (although impractical - why duplicate all the work those two classes already do?)
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

@Lester Burnham
Dude I did not meant what you said is wrong. I am talking about a point (Initiated by Harpreet Singh) which we missed.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Basic Servlet Doubt
 
Similar Threads
How to get server IP address?
what code will be there in init() of GenericServlet
doPost method ?
Cactus question
Clarification abt init()