In this post, we will see how to create a simple servlet, which prints “Hello World” string on the browser. We are using eclipse IDE to create Dynamic web project and the servlet. To run the application, we are using Tomcat server.

Following are the steps to create the servlet.

  • Create a new Dynamic Web Project from eclipse tool bar: File > New > Dynamic Web Project as shown below:

servlet_hw_1.PNG

  • Enter the Project name filed and set the Target run time To installed Apache Tomcat Server path and click on Next as shown below:

servlet_hw_2

  • Click on Next button as shown in following image:

servlet_hw_3

  • Select the check box as shown below, before clicking on Finish button. This will generate Deployment descriptor required for the project in a web.xml file.

servlet_hw_4

  • Now the Dynamic Web Project is created. This project will contain necessary directory structure required for a Dynamic Web application as shown below:

servlet_hw_5

  • Now create a new Servlet class from : File > New > Servlet as shown below:

servlet_hw_6

  • This will open Servlet configuration window. Enter the Java package, in which the servlet class should be created, Servlet class name as shown below. We are creating a HttpServlet by selecting super class as javax.servlet.http.HttpServlet. Finally, click on Next button.

servlet_hw_7

  • Click on Next button again as shown below:

servlet_hw_8

  • Finally, click on Finish button.

servlet_hw_9

  • Now our Servlet class HelloWorldServlet is ready. It contains the following code. Here @WebServlet is the annotation used to define our servlet and URL pattern to access this servlet is /HelloWorldServlet
package com.asb;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 
* Servlet implementation class HelloWorldServlet 
*/
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet { 
 private static final long serialVersionUID = 1L;           
 /**     
 * @see HttpServlet#HttpServlet()     
 */    
public HelloWorldServlet() {        
  super();        
  // TODO Auto-generated constructor stub    
}
 /**
 * @see HttpServlet#doGet(HttpServletRequest request, 
 * HttpServletResponse response) 
 */ 
 protected void doGet(HttpServletRequest request, 
 HttpServletResponse response) throws ServletException, IOException { 
  // TODO Auto-generated method stub 
 }
 /** 
 * @see HttpServlet#doPost(HttpServletRequest request, 
 * HttpServletResponse response) 
 */ 
 protected void doPost(HttpServletRequest request, 
 HttpServletResponse response) throws ServletException, IOException { 
   // TODO Auto-generated method stub doGet(request, response); 
 }
}
  • Now add the following code inside doGet() method of our HelloWorldServlet. Here, we are setting response content type as “text/html“, then we are getting a PrintWriter object to send the response string “This is Hello World Servlet” back to client. Note that we need to import java.io.PrintWriter package to use PrintWriter object.
response.setContentType("text/html"); 
PrintWriter printWriter = response.getWriter(); 
printWriter.println("This is Hello World Servlet!!");
  • Finally, our code should look like following:
package com.asb;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** 
 * Servlet implementation class HelloWorldServlet 
*/
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet { 
 private static final long serialVersionUID = 1L;           
 /**     
 * @see HttpServlet#HttpServlet()     
 */    
 public HelloWorldServlet() {        
  super();       
  // TODO Auto-generated constructor stub    
 }
 /** 
 * @see HttpServlet#doGet(HttpServletRequest request, 
 * HttpServletResponse response) 
 */ 
 protected void doGet(HttpServletRequest request, 
 HttpServletResponse response) throws ServletException, IOException { 
  // TODO Auto-generated method stub 
  response.setContentType("text/html"); 
  PrintWriter printWriter = response.getWriter(); 
  printWriter.println("This is Hello World Servlet!!"); 
 }
 /** 
 * @see HttpServlet#doPost(HttpServletRequest request, 
 * HttpServletResponse response) 
 */ 
 protected void doPost(HttpServletRequest request, 
 HttpServletResponse response) throws ServletException, IOException { 
 // TODO Auto-generated method stub 
 doGet(request, response); 
 }
}
  • Now it’s time to run our servlet. For this, right click on project, and select Run As > Run on Server as shown below:

servlet_hw_10

  • This will run open up a window as shown below. Click on the Finish button. If there is no existing server available. Click on “Manually define a new server” option and select the proper server details to run the Servlet.

servlet_hw_11

  • If our Tomcat server is started properly and servlet is deployed, it will shown as shown below:

servlet_hw_12

  • On clicking Finish button, it will automatically open the browser and string “This is Hello World Servlet!! ” is printed on the browser. Here we have to exactly specify the URL mapping of the Servlet on browser window to access our servlet.

servlet_hw_13

Version Details:

  • Tomcat Server Version 8
  • Eclipse Neon.2 Release (4.6.2)