Hello World Example Use Servlet

Servlet is the most important web component in Java EE Web application. It is also the foundation of many Java EE framework such as Spring, JSF, Struts etc. In this article we will show you how to write a basic hello world servlet java program use eclipse step by step.

Steps to create java servlet use eclipse

  1. Open Eclipse and create a dynamic web application project. If you do not know how to do that, please click above link to learn how to use tomcat and create web application use Eclipse first.
  2. Right click project name, click “New —> Servlet” in popup menu.
    right click project and create servlet in eclipse
  3. Input java package name as com.dev2qa.example.servlet, and class name HelloWorld in next dialog. Please note that your class’s superclass is HttpServlet.
    hello world servlet class properties
  4. Click Next. Input “This is the first web application hello world example.” in description. Please note the url mappings section, we will use this mapping path to access this servlet. The mapping path is the context path relative to the application’s context path. For example, if you access your application by url http://localhost:8080/Dev2qaWebAppExample/, then you need to use http://localhost:8080/Dev2qaWebAppExample/HelloWorld to access the HelloWorld servlet running in the web application.
    hello servlet class detail settings in eclipse
    The initialization parameters is used to save configuration settings such as username and password, java database connection url etc. We will use it later in this example.
  5. Click Next, you will see a lot of servlet life cycle methods in next panel. Just check init(), doGet(), doPost() and destroy() methods, article Servlet Life Cycle has a detail introduction for those methods.
    http sevlet methods
  6. Click Finish button to complete the wizard.
READ :   Http Session Management - Hidden Form Field

You may can not find configuration xml added in web.xml, but you can find below added java code in HelloWorld.java file. This is because Tomcat 7.0 and above versions is servlet 3.0 compatible, and servlet 3.0 will use annotation to replace xml configuration in web.xml.

@WebServlet(
 urlPatterns = { "/HelloWorld" }, 
 initParams = { 
 @WebInitParam(name = "username", value = "Jerry Zhao")
 })
public class HelloWorld extends HttpServlet {
 private static final long serialVersionUID = 1L;

To configure it in web.xml, you need to change the project’s dynamic web module’s version to 2.5 or lower then create another servlet again.

Steps to add servlet configuration in web.xml automatically

  1. Click “Window —> Show View —> Navigator” in eclipse main menu.
    click window_show view_navigator in eclipse main menu
  2. Click “.settings —> org.eclipse.wst.common.project.facet.core.xml” file in left panel. Change facet jst.web’s version value to 2.5 in right panel.
    change dynamic web module version in org.eclipse.wst.common.project.facet.core.xml file
  3. Create another servlet within package com.dev2qa.example.servlet and class name is HelloWorld1. Then you can see below xml definition added in web.xml.

         servlet definition in web.xml

You can also select dynamic web module version 2.5 when you create the web application project.

  1. Click “File —> New —> Dynamic Web Project” in eclipse main menu.
    create java EE web application project in eclipse
  2. Choose dynamic web module version 2.5 in popup dialog.
    choose dynamic web module version when create dynamic web application project

Write java code in Hello World example project

Despite of which dynamic web module version you choose, we will now create java code for our project.

  1. Write below java code in HelloWorld.java’s doGet() method.
     PrintWriter pw = response.getWriter();
    
     pw.println("Hello World in doGet() method.");
    
  2. Right click your project name, click “Debug As —> Debug On Server” to start Tomcat for debugging.
    start debugging web application in tomcat
  3. After Tomcat server startup. Input url http://localhost:8080/Dev2qaWebAppExample/HelloWorld in your favorite web browser. Click Enter. You will see below picture.
    hello world result page in tomcat
  4. Write below java code in HelloWorld.java’s doPost() method.
     PrintWriter pw = response.getWriter();
    
     pw.println("Hello World in doPost() method." + this.userName);
  5. Because we need to get the init parameter username’s value in above java code. So we should add a String type instance variable userName, and get it’s value in init() method. We can only get init parameter in init() method otherwise there will have a null pointer exception occurred in run time.
    private String userName = "";
    
     public void init(ServletConfig config) throws ServletException {
    
     userName = config.getInitParameter("username");
    
     }
  6. Add below html code in index.jsp file which is the welcome file.
    <form action="/Dev2qaWebAppExample/HelloWorld" method="post">
    <input type="submit" value="Submit"/>
    </form>
  7. Browse url “http://localhost:8080/Dev2qaWebAppExample/”, click “Submit” button in the page. Then you will see below picture.
    hello world output string in doPost method
READ :   Java Servlet Filter Introduction

Download “Hello World Servlet Example Code.zip” WebApplicationCode.zip – Downloaded 65 times – 11 KB

(Visited 338 times, 1 visits today)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.