• 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

Is HttpServlet an abstract class or interface?

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public abstract class GenericServlet
To write a generic servlet, you need only override the abstract service method.

public abstract void service(ServletRequest req,
ServletResponse res)
throws ServletException,
java.io.IOException
Called by the servlet container to allow the servlet to respond to a request.
This method is declared abstract so subclasses, such as HttpServlet, must override it.
public abstract class HttpServlet
extends GenericServlet

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?
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic