jump to navigation

Types of Servlets January 23, 2007

Posted by Allu in Servlets.
8 comments

Servlets must implement the interface javax.servlet.Servlet. There are two main types of servlets:

  • Generic servlets extend javax.servlet.GenericServlet. Generic servlets are protocol independent, meaning that they contain no inherent support for HTTP or any other transport protocol. 
  • HTTP servlets extend javax.servlet.HttpServlet. These servlets have built-in support for the HTTP protocol and are much more useful in an Browser environment

Servlets that extend HttpServlet are much more useful in an HTTP environment, since that is what they were designed for. We recommend that all  Servlets extend from HttpServlet rather than from GenericServlet in order to take advantage of this built-in HTTP support.

For both types of Servlets, you can implement the constructor method init() and/or the destructor method destroy() if you need to initialize or deallocate resources. 

All Servlets must implement a service() method. This method is responsible for handling requests made to the Servlet. For generic Servlets, you simply override the service() method to provide routines for handling requests. HTTP Servlets provide a service method that automatically routes the request to another method in the servlet based on which HTTP transfer method is used, so for HTTP Servlets you would override doPost() to process POST requests, doGet() to process GET requests, and so on.

Difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface? January 3, 2007

Posted by Allu in Servlets.
2 comments

The getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a “/” it is interpreted as relative to the current context root.
The getRequestDispatcher(String path) method of javax.servlet.ServletContext interface cannot accepts relative paths.