Servlet hello world example

This tutorial we will define you the Servlet, Servlet life cycle and a Servlet hello world program.

Servlet hello world example

Servlet  hello world example

This tutorial will help you to understand the Servlet from scratch. Servlet is a server side programming language. Servlet are used to create web application. Which implement javax.servlet.Servlet interface. Servlet are responsible to handle web request. The javax.servlet and javax.servlet.http package provide interface and classes for writing Servlet. The Servlet classes must implements Servlet interface. The HttpServlet class provide methods, such as doGet and doPost to handle the Http services.

Servlet can be described in many ways as follows:

  • Servlet is used to create web application.
  • Servlet is an interface that must be implented for creating servlet.
  • Servlet is a web component that is deployed on the server  to create dynamic web page.
  • Servlet provide many interface and classes.

Servlet life cycle has three method init(), service() and destroy() which must be implemented by every Servlet.

  • While initialization of Servlet life cycle web container initialize the Servlet by calling init() method of Servlet class.
  • After initialization of Servlet , Servlet instance can service client request. Each service is serviced in their own thread. The web container call the service() method of the Servlet for every request. Service method will find the kind of request and dispatch it to appropriate method to handle the request.
  • At last destroy() is called to that take the Servlet out and one more point destroy() is called only once like init() method.

These are the three method of  Servlet life cycle. We should start understanding Servlet by making Servlet hello world program. We will just write one program which print hello world in Servlet. Let us explain this by a hello world example using Servlet.

Example :  Hello world example in Servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Sevlet Hello World</title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
}

While creating Servlet you need to do mapping of Servlet using xml. In this web.xml  file servlet class name have to provide in the Servlet-class tag and  in the url -pattern it is advisable to provide the /servlet_class-name or you can give any name while creating servlet it should be same like /ServletDemo. Now here is the web.xml file as below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"> -->

<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>ServletDemo</servlet-class> 
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/ServletDemo</url-pattern>
</servlet-mapping>
</web-app>

 Output from the program :

Download Source Code