Hello World Servlet using Eclipse and Tomcat

In this post, we will see how to create a servlet application. We are using Eclipse IDE to create a Dynamic web project and the servlet. To run the application, we are using the Tomcat server.

Version Details:

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

Table of Contents

Creating the servlet application using Eclipse

Following are the steps to create the servlet.

Create a new Dynamic Web Project from the eclipse toolbar: File > New > Dynamic Web Project as shown below.

servlet_hw_1.PNG

Enter the Project name field and set the Target run time path as the installed Apache Tomcat Server path. Finally, click on the Next button as shown below.

servlet_hw_2

Click on the Next button as shown in the following image.

servlet_hw_3

Select the check box as shown below, before clicking on the Finish button. This will generate the 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 the 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

Writing the first servlet

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); 
 }
}

Deploying the servlet to tomcat on eclipse

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

Conclusion

In this article, we learned how to create a servlet application using the Eclipse IDE.

We also learned how to deploy and run the application on an apache tomcat server.