HelloWorld using Servlet and Eclipse IDE

In this post We will see very basic example of HelloWorld using Servlet, Eclipse IDE and run the application using Tomcat 7 Server. Here We will follow the step by step process of simply printing the message using servlet.

So the basic requirement to run this project is Eclipse ide and Tomcat server, so in case if you don’t have any of them, then you can go to the respective project site using the below links and download the required software.

Download Eclipse IDE
Download Tomcat Server 7

Now open the eclipse ide go to File –> New –> Dynamic Web Project as depicted in image below


Give Project name like Servlet-HelloWorld and click next next and check the Generate web.xml deployment descripter as depicted in image below

project name

We will have the project structure as shown in image below


Now We will write the code for our servlet class, create a package named javaArtifacts under src folder and crate a class HelloWorld.java inside javaArtifacts package. code of the class will be like shown below

package javaArtifacts;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class HelloWorld extends GenericServlet {

	private static final long serialVersionUID = 1L;

	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("<b>GenericServlet class defines a generic, protocol-independent servlet</b>");  


In the above class we are extending the GenericServlet class and overriding it’s service method having the request and response object. Setting the response using the response.setContentType(), getting the PrintWriter object from response.gertWriter(). Sending back the response and call the close() method of PrintWriter.

Now after writing the code you are seeing the error on GenericServlet in Eclipse ide that means you do not have servlet-api.jar in your project. So download the jar file. After downloading put the jar file inside the lib folder of your project. By following these steps We have created the servlet class which will be compiled and run using the tomcat server. Now in order to run this application we will make the entry of that servlet in web.xml Deployment descriptor file as shown below

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

here we are mapping servlet class with servlet name helloworld and servlet name helloworld with url pattern /helloworld. we have completed all the steps to create a simple servlet, run this project using the tomcat server and fire the below url in the browser http://localhost:8080/Servlet-HelloWorld/helloworld. If all goes well you will see the message Hello GenericServlet class defines a generic, protocol-independent servlet

Download Servlet-HelloWorld Eclipse Project

Leave a Reply

Your email address will not be published. Required fields are marked *