HttpServlet class in java

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 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 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 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 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 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 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 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 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 time is unknown.
Syntax:

protected long getLastModified(HttpServletRequest req)

Servlet “Hello World” example by extending 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;
 
/**
 * This servlet program is used to print "Hello World" 
 * on client browser using HttpServlet class.
 * @author w3spoint
 */
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("<h1>Hello World using HttpServlet class.</h1>");
        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.w3spoint.business.HelloWorld
		</servlet-class>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/HelloWorld</url-pattern>
	</servlet-mapping>
 
</web-app>

Output:


servlet example 3
 
Download this example.
 
Next Topic: Deployment Descriptor: web.xml file with example.
Previous Topic: GenericServlet class in java with example.

 

Content Protection by DMCA.com
Please Share