Open In App

HttpServlet Class In Java

HttpServelt is an abstract class, it comes under package ‘javax.servlet.http.HttpServlet‘ . To create a servlet the class must extend the HttpServlet class and override at least one of its methods (doGet, doPost, doDelete, doPut). The HttpServlet class extends the GenericServlet class and implements a Serializable interface. 

Constructor of HttpServlet Class



HttpServlet()

This is an abstract class so, the constructor does nothing.

Methods of HttpServlet Class

1. doGet() Method

Modifier and Type:  protected void



Syntax:

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException

Parameters:

Exceptions:

2. doPost() Method

Modifier and Type:  protected void

Syntax:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException

Parameter:

Throws:

3. doHead() Method

Modifier and Type: protected void 

Syntax:

protected void doHead(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException

Parameters:

Exceptions:

4. doPut() Method

Modifier and Type:  protected void

Syntax:

protected void doPut(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException

Parameters:

Throws:

5. doDelete() Method

Modifier and Type:  protected void 

Syntax:

protected void doDelete(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException

Parameters:

Exceptions:

6. doOptions() Method

Modifier and Type: protected void 

Syntax:

protected void doOptions(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException

Parameters:

Exceptions:

7. doTrace() Method

Modifier and Type:  protected void

Syntax:

protected void doTrace(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException

Parameters:

Exceptions:

8. getLastModified() Method

Modifier and Type: protected long

Syntax:

protected long getLastModified(HttpServletRequest request)

Parameter: request – an HttpServletRequest object that contains the request the client has made of the servlet.

9. service() Method

This method receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class.

Modifier and Type: protected void

Syntax:

protected void service(HttpServletRequest request, HttpServletResponse response) 
throws ServletException,IOException

Parameters:

Exceptions:

10. service() Method

This method is used to dispatch client requests to the public service method.

Modifier and Type: public void

Syntax:

public void service(ServletRequest request,ServletResponse response) 
throws ServletException,IOException

Parameters:

Exceptions:

Example:




package com.gfg;
import javax.servlet.*;
import javax.servlet.http.*;
 
// here GFGServlet class inherit HttpServlet
public class GFGServlet extends HttpServlet {
 
    // we are defining the doGet method of HttpServlet
    // abstract class
    public void doGet(HttpServletRequest rq,
                      HttpServletResponse rs)
    {
        // here user write code to handle doGet request
    }
 
    // we are defining the doPost method of HttpServlet
    // abstract class
    public void doPost(HttpServletRequest rq,
                       HttpServletResponse rs)
    {
        // here user write code to handle doPost request
    }
 
} // class ends

Note: This is server-side code, it is only for clarification on how to use HttpServlet class.


Article Tags :