• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Basic Servlet Doubt

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rakesh Maddala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GenericServlet is used if you want to implement non http service.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic