What are different types of Servlet?

There are three approaches to write a servlet:
Approach 1:
By implementing javax.servlet.Servlet
Public class MyServlet implements Servlet{
Public void init(ServletConfig sc){}
Public void service(ServletRequest request,ServletResponse response){}
Public void destroy(){}
Public ServletConfig getServleConfig(){}
Public String getServletInfo(){}
}
Approach 2:
By extending javax.servlet.GenericServlet(subclass of javax.servlet.Servlet)
Public class MyServlet extends GenericServlet{
Public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException{}
}
Approach 3:
By extending javax.servlet.HttpServlet(subclass of GenericServlet)
Public class MyServlet extends HttpServlet{
Public void doXXX()throws ServletException,IOException{}
}

GenericServlet is used for general purpose but HttpServlet understand only HTTP protocol.
HttpServlet is mostly used for web applications.

No comments:

Post a Comment