HttpServlet class:
HttpServlet class extends the GenericServlet. It is protocol-dependent.
public abstract class HttpServlet extends GenericServlet
HttpServlet class is in javax.servlet.http package (javax.servlet.http.HttpServlet).
Methods of HttpServlet class:
1. service(ServletRequest req,ServletResponse res): Dispatches the requests to the protected service method. It converts the request and response object into HTTP type before dispatching the request.
Syntax:
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
2. service(HttpServletRequest req, HttpServletResponse res): Receives HTTP requests from the public service method and dispatches the request to the doXXX methods defined in this class.
Syntax:
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
3. doGet(HttpServletRequest req, HttpServletResponse res): This method is called by the web container for handling GET requests.
Syntax:
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
4. doPost(HttpServletRequest req, HttpServletResponse res): This method is called by the web container for handling POST requests.
Syntax:
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
5. doHead(HttpServletRequest req, HttpServletResponse res): This method is called by the web container for handling HEAD requests.
Syntax:
protected void doHead(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
6. doOptions(HttpServletRequest req, HttpServletResponse res): This method is called by the web container for handling OPTIONS requests.
Syntax:
protected void doOptions(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
7. doPut(HttpServletRequest req, HttpServletResponse res): This method is called by the web container for handling PUT requests.
Syntax:
protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
8. doTrace(HttpServletRequest req, HttpServletResponse res): This method is called by the web container for handling TRACE requests.
Syntax:
protected void doTrace(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
9. doDelete(HttpServletRequest req, HttpServletResponse res): This method is called by the web container for handling DELETE requests.
Syntax:
protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
10. getLastModified(HttpServletRequest req): It returns the time the HttpServletRequest object was last modified since midnight January 1, 1970 GMT. It returns a negative number if the time is unknown.
Syntax:
protected long getLastModified(HttpServletRequest req)
Servlet “Hello World” example by extending the HttpServlet class.
HelloWorld.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
//no-argument constructor.
public HelloWorld() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World using HttpServlet class.");
out.close();
}
}
web.xml
<!--?xml version="1.0" encoding="UTF-8"?-->
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>
com.w3schools.business.HelloWorld
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>