Java Web Application - Hello World

This tutorial shows you the way to create your first Hello World web application in Java. You need to have Eclipse IDE for Java EE Developers and Apache Tomcat, in order to develop this application. If you have not configured the Apache Tomcat in Eclipse, follow this tutorial and configure it first.

Step 1:
Go to File → New → Project.

Step 2:
Under the Web category, select the Dynamic Web Project option and click on Next.

Step 3:
Give the project name 'Hello World' and click on the Finish button.

Step 4:
Right click on the src folder of 'Hello World' project and select New → Servlet.

Step 5:
Give a package name 'com.javahelps.helloworld' and class name 'HelloWorldServlet' as shown in the screenshot.
Click on the Finish button to create the Servlet.

Step 6:
Modify the Servlet class code as provided below.
package com.javahelps.helloworld;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

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("/saytime")
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 {
        PrintWriter out = response.getWriter();
        out.print("<html><body><h1 align='center'>" +
        new Date().toString() + "</h1></body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}
Here the @WebServlet annotation defines the URL of this Servlet. The doGet method is used to serve the GET requests and doPost method is used to serve the POST requests. According to this code, this Servlet can accept both GET and POST requests but it produces the output only for GET requests. Any POST requests will cause to a blank screen in the browser. PrintWriter is used to write the current time as an HTML content to the response.

Step 7:
Right click on the WebContent folder and create a new HTML file index.html.


Step 8:
Modify the HTML file as shown below.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<a href="saytime">Click Here</a>
</body>
</html>
The hyperlink reference is pointing to the sayhello URL which is defined in the HelloWorldServlet class.

Step 9:
After saving all the changes, right click on the project and select Run As → Run on Server.

Step 10:
Select the Tomcat server and click on the Finish button to run the application.

Now your first application is available at the following link:
http://localhost:8080/Hello_World/



Find the project at Git Hub.
Previous
Next Post »

Contact Form

Name

Email *

Message *