• Javax.servlet. A servlet is an interface that is the root for all the servlets you are developing.
• Javax.servlet.GenericServlet class is a direct subclass of the servlet interface.
• GenericServlet is overriding all the methods of the Servlet interface except service () method because GenericServlet is declared as abstract.
• When you develop the servlet by extending GenericServlet, you have to override the service() method as follows:
Public void service(ServletRequest req, ServletRequest res)->GENERIC SERVICE
}
class HelloServlet extends GenericServlet{
public void service(HttpServletRequest req, HttpServletResponse res){
//processing code
}
}
• The Javax.servlet.http.HttpServlet class is the direct subclass of GenericServlet.
• HttpServlet overrides all the methods of GenericServlet, but HttpServlet isdeclared as abstract.
• When you develop the servlet by extending HttpServlet, you can override any of the following nine methods:
o void service(HttpServletRequest req, HttpServletResponse res)
o void doGet(HttpServletRequest req, HttpServletResponse resp)
o void doPost(HttpServletRequest req, HttpServletResponse resp)
o void doPut(HttpServletRequest req, HttpServletResponse resp)
o void doDelete(HttpServletRequest req, HttpServletResponse resp)
o void doHead(HttpServletRequest req, HttpServletResponse res)
o void doTrace(HttpServletRequest req, HttpServletResponse res)
o void doOptions(HttpServletRequest req, HttpServletResponse res)
• Whatever the Servlet class you are extending or whatever the method you are overriding, container always calls the GENERIC SERVICE method.
Class cls=Class.forName("com.jtcindia.HelloServlet");
Object obj=cls.newInstance();
Servlet s=(Servlet)obj;
s.service(req,res);
Case 1: I have written HelloServlet by extending HttpServlet and overridden Generic service method.
interface Servlet{
public abstract void service(ServletRequest req,ServletResponse res) ;
}
abstract class GenericServlet implements Servlet{
public abstract void service(ServletRequest req,ServletResponse res) ;
}
abstract class HttpServlet extends GenericServlet{
public abstract void service(ServletRequest req,ServletResponse res) ;
//some code
}
class HelloServlet extends HttpServlet {
public abstract void service(HttpServletRequest req, HttpServletResponse res){
//some code
}
}
Explanation:
Class cls=Class.forName("com.jtcindia.HelloServlet");
Object obj=cls.newInstance();
Servlet s=(Servlet)obj;
s.service(req,res);
• Container calls the generic service method, and the generic service method, which is overridden in the HelloServlet, will be called.
Case 2: I have written HelloServlet by extending HttpServlet and overriding the HTTP service method.
Class HelloServlet extends GenericServlet {
@Override
public void service(ServletRequest req, ServletResponse res)
{
HttpServletRequest req1=(HttpServletRequest)S.req;
HttpServletResponse req2=(HttpServletResponse)S.res;
service(req1,req2);
}
protected void service(ServletRequest req, ServletResponse res){
//some code
}
….
}
Class HelloServlet extends HttpServlet(){
protected void service(ServletRequest req, ServletResponse res){
//some code
}
}
Explanation:
• Container calls the generic service method.
• The generic service method of HttpServlet, which is inherited from HelloServlet, will be called.
• The generic service method of HttpServlet invokes the HTTP service method.
• The generic service method of HelloServlet will be called.
Case 3: I have written HelloServlet by extending GenericServlet and Override Generic service method.
class HttpServlet extends GenericServlet {
@Override
protected void service(ServletRequest req, ServletResponse res)
{
//some code
}
}
Explanation:
You will get compile time error.
Case 4: I have written HelloServlet by extending HttpServlet and overridden HTTP service method and Generic service method.
class HttpServlet extends GenericServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//some code
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
//some code
}
}
Explanation:
• Container calls the generic service method.
• The generic service method, which is overridden in HelloServlet, will be called.
Case 5: I have written HelloServlet by extending HttpServlet and overriding doGet() method.
class HttpServlet extends GenericServlet {
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
//calls http service method
}
}
protected void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
String m=req.getMethod();
doGet(req,res);
}else if(m.equals(“POST”)){
doPost(req,res);
}
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//send the error with Http status Code:405
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//send the error with Http status Code:405
}
class HelloServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
//send the error with Http status Code:405
}
Explanation:
• Container calls the generic service method.
• The generic service method of httpService, which is inherited from HelloServlet, will be
called.
• The generic service method of HttpServlet invokes the HTTP service method.
• The HTTP service method of HttpServlet will be called.
• The HTTP service method of HttpServlet checks the incoming request and invokes the corresponding doXX() method.
A. doGet() method will be called from the HelloServlet.
B. The doPost() method will be called from the HelloServlet.
• HTTP Status 405: The HTTP method POST is not supported by this URL.
Note:
• When you override the GENERIC SERVICE method in your method, then that will be called for any incoming request http method.
• When you override the HTTP SERVICE method in your servlet, then only that will be called for any incoming request http method.
• When you override any doXX() method in your servlet, then that method will be called as per matching the incoming request's http method.