CodesJava

Easy learning with example program codes

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 codesjava
 */
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.codesjava.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.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 



Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki