aspose file tools*
The moose likes Servlets and the fly likes who provides functionality.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "who provides functionality...." Watch "who provides functionality...." New topic
Author

who provides functionality....

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

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


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Ulf Dittmer. I will better look into JavaDocs...
Kumar Garlapati
Ranch Hand

Joined: Feb 04, 2008
Posts: 38
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.


Regards,<br />Kumar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: who provides functionality....
 
Similar Threads
Implementing methods from HttpServlet
protected Method service???
servlets
Why 2 service() methods in HttpServlet??
Is HttpServlet an abstract class or interface?