Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

who provides functionality....

 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the 100th page of Head First Servlets & JSP book, we have the following

The container calls my servlet's init() method, but if I dont override init(), the one from GenericServlet runs. Then when a request comes in, the container starts or allocates a thread and calls the service() method, which I dont override. so the service() method from HttpServlet runs. The HttpServlet service() method then calls my overridden doGet() or doPost().



My Question from the above para is:
The Httpservlet and GenericServlet are abstract classes. So obviously its function doesnt have body. The Servlet is an interface.

Suppose I override only doGet()/doPost() method,
1. how does the init() method runs from GenericServlet which doesnt have any body part?
2. the same way how does the service() method runs from Httpservlet which is also an abstract class?
3. who will provide the functionality for these methods?
4. Can anybody tell the flow of call.....


Thanks in Advance

Siva
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Httpservlet and GenericServlet are abstract classes. So obviously its function doesnt have body.

That's not obvious to me. In fact, all methods are implemented (check the javadocs). Declaring the class abstract merely means that it needs to be extended in order to be used, not that any methods are abstract.
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer. I will better look into JavaDocs...
 
Kumar Garlapati
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Httpservlet and GenericServlet are abstract classes. So obviously its function doesnt have body. The Servlet is an interface.

HttpServlet is an abstract class, obviously its function doesn't have body is a wrong statement, all functions of HttpServlet got body, the only thing is, the class is abstract, so that no other can create an object of this class and calls the methods manually.

GenericServlet is an abstract class, similar to HttpServlet, all the methods got functionality except service() method. which we have to implement the body, if your class extends GenericServlet.

Servlet is an interface, which obviously you have to implement the methods if your class implements it.

Last but not least point: init(), service() and destroy() are call back methods, which server will call on its own, depends on request.

Replies for your questions:
1)http://www.docjar.com/html/api/javax/servlet/GenericServlet.java.html
2)You might have got it from my above explaination.
3)SUN already provided(look into spec)
4)If you class overrides only doGet/doPost, they will not be called directly by the server. As is said these are not life cycle/call back methods. all the servlets directly/indirectly implements the functionality of service() method, in which we have code like below:

public void service(request,response){
String str = request.getMethod();
if(str.equals("POST"))
doPost(request,response);
if(str.equals("GET"))
doGet(request,response);
}

the above code is written by SUN/Vendor, now you no need to bother about service method, you have to just concern about your doPost or doGet methods, which will get called automatically by service() method.

it's not copy past guys, I keyed everything. Please correct me if I went wrong.

Hope this explanation helps you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic