Is HttpServlet an abstract class or interface? If yes,where are we implementing all methods of HttpServlet in our servlet? As JAVA API says all methods in the abstract class or interfaces should be implemented in its subclass,otherwise that class will become abstrat.. If HttpServlet is abstract class or interface and we are not implementing all methods of it, my servlet should become abstract ..Why we are not following this in servlet?
HttpServlet class is an abstract class but it does nothave any abstract methods.But for proper functioning we normally override the doGet or doPost because these methods are called by the service method of the servlet depending upon the request received. Veena
GenericServlet is an abstract class and its service() method is abstract. So in case you extend this class, you need to provide an implementation of the service() method. However, there are many interfaces in HttpServlet and JSP/custom tag packages (like HttpServletRequest, etc..). Many times we don't provide any implementation of the interface methods. In such situations, the container provides a default implementation. This I've see true even when we're playing with EJBs. Whenever you wonder how an application works without implementing some interace methods, it is the CONTAINER which is the answer. Thanks, Sudd
SCJP 1.4, SCWCD, SCBCD 1.3
Joined: Feb 01, 2003
public abstract class GenericServlet To write a generic servlet, you need only override the abstract service method.
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site This info I got it from SERVLET API ,It is clearly mentioned that service method is abstract method. 1)How is this method overriden in HttpServlet Or user-defined servlet? Since service method is abstract so is GenericServlet class . 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?
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 22.214.171.124 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 ]