• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling doGet of Child class from the Service() of Parent Class.

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

regarding the lifecycle of servlet , in headfirst servlet i can find :

You normally will NOT override the service() method, so the one from HttpServlet will run. The service() method figures out which HTTP method (GET, POST, etc.) is in the request, and invokes the matching doGet() or doPost() method. The doGet() and doPost()
inside HttpServlet don’t do anything, so you have to override one or both. This thread dies (or is put back in a Container-managed pool) when service() completes.


now my question is how can i call the doGet method of the subclass from the superclass. i am not getting this .
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simple answer is you don't need to. The doGet() or doPost() method will be called automatically assuming you haven't overridden service().

As to how you call the one in the sub class, again this is handled for you. Whenever the method is called (doGet or doPost) the version in your sub class will be called because it overrides the method provided in the super class.
 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:The simple answer is you don't need to. The doGet() or doPost() method will be called automatically assuming you haven't overridden service().

As to how you call the one in the sub class, again this is handled for you. Whenever the method is called (doGet or doPost) the version in your sub class will be called because it overrides the method provided in the super class.


But it is the service method which decides whether to implement doGet or doPost or....
AND --- i cant access the child class method from parent class unless i create child class object .
(please correct my understanding of inheritance if its wrong ). then how it automatically calls the doGet of my custom myservlet.java from the service method which is present in HttpServlet interface .

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question too hard for “beginning”: moving discussion.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your servlet is a subclass of HttpServlet. So when the servlet container receives a request for your servlet, it creates an instance of it and calls the service() method of that instance. The instance already has a service() method, because HttpServlet declares one. That method decides whether to call doGet() or doPost() depending on the request, and you're going to override one of those methods with your code, so don't muck things up by overriding HttpServlet's version of service().

Another option: just write servlets in the normal way and implement doGet() and/or doPost() appropriately. Don't implement service() because it's not necessary for you to do that. You'll find that works perfectly well.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

luke brown wrote:
AND --- i cant access the child class method from parent class unless i create child class object .
(please correct my understanding of inheritance if its wrong ).



Yes, your understanding of inheritance is wrong. There is no "parent class object" and "child class object" -- there is only one object. In your case that object is both a LukeBrownServlet and an HttpServlet (and a GenericServlet and an Object) for that matter. The code for the service() method which is declared in HttpServlet calls the doGet() method of the object, so if you override that in your LukeBrownServlet, that's what it calls.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

luke brown wrote: i cant access the child class method from parent class unless i create child class object .


Your servlet is the subclass. Stop thinking of the class hierarchy as parent/child, because it's not. Think in terms of superclass/subclass. When you create an instance of the subclass, it inherits from the superclass -- there is not a superclass and a subclass object. Just the subclass object which inherits from the superclass.

As already pointed out, this is basic object orientation. Servlets act like any other Java class -- they are not different in any way.

So your servlet has your doGet() and doPost() methods, and it has a service() method that it inherited. The service() method will invoke the appropriate "do" method according to the HTTP method requested.
reply
    Bookmark Topic Watch Topic
  • New Topic