HttpServlet Class In Java
Last Updated :
02 Mar, 2023
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
- This method is used to handle the GET request on the server-side.
- This method also automatically supports HTTP HEAD (HEAD request is a GET request which returns nobody in response ) request.
- The GET type request is usually used to preprocess a request.
Modifier and Type: protected void
Syntax:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
Parameters:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Exceptions:
- IOException – if an input or output error is detected when the servlet handles the GET request.
- ServletException – Use to handle the GET request.
2. doPost() Method
- This method is used to handle the POST request on the server-side.
- This method allows the client to send data of unlimited length to the webserver at a time.
- The POST type request is usually used to post-process a request.
Modifier and Type: protected void
Syntax:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
Parameter:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Throws:
- IOException – if an input or output error is detected when the servlet handles the POST request.
- ServletException – Use to handle the POST request.
3. doHead() Method
- This method is overridden to handle the HEAD request.
- In this method, the response contains the only header but does not contain the message body.
- This method is used to improve performance (avoid computing response body).
Modifier and Type: protected void
Syntax:
protected void doHead(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
Parameters:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Exceptions:
- IOException – if an input or output error is detected when the servlet handles the HEAD request.
- ServletException – Use to handle the HEAD request.
4. doPut() Method
- This method is overridden to handle the PUT request.
- This method allows the client to store the information on the server(to save the image file on the server).
- This method is called by the server (via the service method) to handle a PUT request.
Modifier and Type: protected void
Syntax:
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
Parameters:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Throws:
- IOException – if an input or output error is detected when the servlet handles the PUT request.
- ServletException – Use to handle the PUT request.
5. doDelete() Method
- This method is overridden to handle the DELETE request.
- This method allows a client to remove a document or Web page from the server.
- While using this method, it may be useful to save a copy of the affected URL in temporary storage to avoid data loss.
Modifier and Type: protected void
Syntax:
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
Parameters:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Exceptions:
- IOException – if an input or output error is detected when the servlet handles the DELETErequest.
- ServletException – Use to handle the DELETE request.
6. doOptions() Method
- This method is overridden to handle the OPTIONS request.
- This method is used to determine which HTTP methods the server supports and returns an appropriate header.
Modifier and Type: protected void
Syntax:
protected void doOptions(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
Parameters:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Exceptions:
- IOException – if an input or output error is detected when the servlet handles the OPTIONS request.
- ServletException – Use to handle the OPTIONS request.
7. doTrace() Method
- This method is overridden to handle the TRACE request.
- This method returns the headers sent with the TRACE request to the client so that they can be used in debugging.
Modifier and Type: protected void
Syntax:
protected void doTrace(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
Parameters:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Exceptions:
- IOException – if an input or output error is detected when the servlet handles the TRACE request.
- ServletException – Use to handle the TRACE request.
8. getLastModified() Method
- This method returns the time the HttpServletRequest object was last modified.
- If time is unknown the method will return a negative number.
- This method makes browser and proxy caches work more effectively.
- also reducing the load on server and network resources.
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:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Exceptions:
- IOException – if an input or output error is detected when the servlet handles the HTTP request.
- ServletException – Use to handle the HTTP request.
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:
- request – an HttpServletRequest object that contains the request the client has made of the servlet.
- response – an HttpServletResponse object that contains the response the servlet sends to the client.
Exceptions:
- IOException – if an input or output error is detected when the servlet handles the HTTP request.
- ServletException – Use to handle the HTTP request.
Example:
Java
package com.gfg;
import javax.servlet.*;
import javax.servlet.http.*;
public class GFGServlet extends HttpServlet {
public void doGet(HttpServletRequest rq,
HttpServletResponse rs)
{
}
public void doPost(HttpServletRequest rq,
HttpServletResponse rs)
{
}
}
|
Note: This is server-side code, it is only for clarification on how to use HttpServlet class.
Similar Reads
Java.net.HttpURLConnection Class in Java
HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol. It is one of the popular choic
5 min read
Servlet - Output Stream Class
ServletOutputStream class is a component of Java package javax.servlet, is an abstract class that provides an output stream to send binary data to the client. ServletOutputStream inherits OutputStream which is the superclass of all classes representing an output stream of bytes. Subclasses of Servle
4 min read
Servlet - HTTP Status Codes
For each HTTP request and HTTP response, we have messages. The format of the HTTP request and HTTP response messages are similar and will have the following structure â An initial status line + CRLFCRLF = Â ( Carriage Return + Line Feed i.e. New Line )Zero or more header lines + CRLFA blank line, i.e
5 min read
Generic Servlet Class
GenericServlet implements the Servlet interface and provides an implementation for all its method except the service() method hence it is abstract. GenericServlet class defines a protocol-independent(HTTP-less) servlet. However, while building a website or an online application, we may want to have
3 min read
Javax.servlet.http.Cookie class in Java
Many websites use small strings of text known as cookies to store persistent client-side state between connections. Cookies are passed from server to client and back again in the HTTP headers of requests and responses. Cookies can be used by a server to indicate session IDs, shopping cart contents,
7 min read
java.net.CookieHandler Class in Java
The object of the CookieHandler Class in Java provides a callback mechanism for hooking up an HTTP state management policy implementation into the HTTP protocol handler. The mechanism of how to make HTTP requests and responses is specified by the HTTP state management mechanism. A system-wide Cookie
2 min read
Servlet - Packages
Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver. A package in servlets contains numerous classes and i
6 min read
Servlet - Client HTTP Request
When the user wants some information, he/she will request the information through the browser. Then the browser will put a request for a web page to the webserver. It sends the request information to the webserver which cannot be read directly because this information will be part of the header of t
3 min read
Servlet - War File
A war (web archive) A web project's files are contained in this file. It may contain files such as servlet, xml, jsp, image, html, css, and js. Here, we'll go through what a war file is, how to make one, how to deploy one, and how to remove one. What is WAR File? The WAR file (Web Application Resour
2 min read
Classes and Objects in Java
In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
12 min read