This simple tutorial explains you, how you can run your first Servlet class that will print "Hello World!" string in the browser. We will use Eclipse IDE to develop the code and Tomcat webserver to run the servlet code.

Technologies used in this article

1. Create Dynamic Web Project

Select from the menu File --> New --> Dynamic Web Project.
Create Dynamic Web Project

Enter "HelloWorldServlet" as the project name. Keep rest of the settings as it is as shown in the following screenshot.
Enter Servlet project name

Click "Next" button.
Configure project

Click "Next" button.
Configure web module setting

Check 'Generate web.xml deployment descriptor' checkbox and click "Finish" button and Eclipse IDE will generate the web project automatically as shown below
Generated web project

2. Create Servlet Class

Select from the menu File --> New --> Servlet.
Create Servlet Class

Write "com.srccodes.example" in the 'Java Package' field and "HelloWorld" in the 'Class Name' field. Click 'Next' button.
Define Servlet class

We can specify deployment descriptor (web.xml) specific information in the following screen. Just keep every thing as it is for the time being. Click "Next" button.
Configure deployment descriptor

Click 'Next' button.
specify modifier, interface

Eclipse will generate a Servlet class based on the configuration / input we provided in the previous steps and open the same in the Java Editor as shown below

File: HelloWorld.java


package com.srccodes.example;
 
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 HelloWorld
 */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        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
    }
 
}

Note: Notice the highlighted line number 13 in the above code. "/HelloWorld" is the servlet url that we need to specify in the browser url to access the same. This is annotation based approach to define servlet mapping. We can do the same in web.xml file as well. For simplicity we are moving forward with the annotation based approach that Eclipse generated for us.

4. Write Custom Code

Add your code inside 'doGet' method. 'setContentType' method of HttpServletResponse sets content type of the response to 'text/html' which is the standard MIME content type for Html pages. 'getWriter' method of the response object returns a PrintWriter object. This will be used to print our "Hello World!" string in the browser.

Edit the generated 'HelloWorld.java' as per the following code.

File: HelloWorld.java


package com.srccodes.example;
 
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 HelloWorld
 */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter printWriter = response.getWriter();
        printWriter.println("<h1>Hello World!</h1>");
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
 
}

5. Run Your Servlet Code

Right click on the project 'HelloWorldServlet' and select from context menu 'Run As' --> 'Run on Server'.
Run on Server

Select the existing tomcat server. If not available then manually define a new web server.
Run on Server

Click "Finish" button. HelloWorldServlet web application will be deployed in the tomcat web server.
Deployed web application

6. Browser Output

Eclipse will open a browser and your server side code will print 'Hello World!' in the browser.
Browser Output

Download SrcCodes

All code samples shown in this post are available in the following link HelloWorldServlet.zip

References