Originally posted by thomas davis:
1)How is this method overriden in HttpServlet Or user-defined servlet?
Since service method is abstract so is GenericServlet class .
Yes, service() in GenericServlet is abstract, and thus so is GenericServlet. The HttpServlet class defines the service() method required by Servlet interface. You can verify this simply by looking at the Javadoc for HttpServlet. There you can see that HttpServlet is declared as an abstract class and not an interface, but that the service() method (and every other method) in HttpServlet is not abstract. You can also verify this by obtaining the servlet API source code. You can find the API source code at
http://jakarta.apache.org/site/cvsindex.html
2) If service method implemented in HttpServlet class by CONTAINER, Why HttpServlet class is a abstract class?
3) Moreover there are no abstract methods in HttpServlet class,why is it called abstract class?
4) If container override the method service() in HttpServlet,why is it's access specifier abstract?
As per specification,if we implement the abstract method in its subclass ,no need of mentioning it as a abstract class. Why is it mentioned as a abstract class?
First, the container does not override the service() method. The servlet API implementation provided by the container has the HttpServlet class which contains the service() method. This servlet API can be provided as part of the servlet container. However, nothing prevents you from obtaining a servlet API implementation from some third party and using that with the servlet container. In
tomcat 4.1 and earlier, the API binary was in servlet.jar; with Tomcat 5.0, it is now servlet-api.jar.
Second, I'm not sure which spec you are referring to. The Java Lang Spec, P 8.1.2.1 requires that abstract classes to be declared abstract: "If a class that is not abstract contains an abstract method, then a compile-time error occurs." In other words, if the class has an abstract method, the class MUST be declared abstract. However, the spec does not prevent a class with NO abstract methods from being declared abstract. There are other classes in Java like that (for instance, look at the adapter classes like java.awt.event.MouseAdapter).
Which access specifier is abstract? The access specifier for service() is public void for the service() method required by Servlet, and protected void for the overloaded version provided by HttpServlet. The HttpServlet class is declared to be abstract, because you are expected to implement your own servlet class.
The HTTP spec defines various HTTP methods such as GET, POST, etc. HttpServlet provides doGet() and doPost() and various other methods to respond to HTTP Requests. However, doGet and doPost are implemented to return an error message to the user saying GET not supported, or POST not supported. You are expected to extend HttpServlet and implement doPost and/or doGet and/or any of the other methods to provide an implementation that is meaningful to your web application. HttpServlet is declared abstract to force you to implement your own servlet class, where you will presumably implement your own doPost or doGet method.
HttpServlet defines NO abstract methods. However, the designers of Servlets did not want you to simply use HttpServlet as your servlet class. Thus, to force you to implement your own servlet class, the class is marked as abstract.
[ February 07, 2003: Message edited by: Kevin Mukhar ]