Java Servlet and JSP Hello World Tutorial with Eclipse, Maven and Apache Tomcat
- Details
- Written by Nam Ha Minh
- Last Updated on 11 March 2020   |   Print Email
- JDK 8
- Servlet 3.1
- JSP 2.3
- Eclipse 4.6 (Neon)
- Apache Tomcat 8
1. Creating a Java Dynamic Web Project
In Eclipse IDE, click menu File > New > Dynamic Web Project to create a project for Java web application. Name the project as HelloWorldJavaEE and keep the default settings for:- Target Runtime: Apache Tomcat v8.0
- Dynamic web module version (Servlet version): 3.1
- Configuration: Default Configuration for Apache Tomcat v8.0



2. Adding Dependencies for Serlvet and JSP in Maven’s Project File
Now, we need to add Maven as a build manager for our project. To do so, right click on the project and select Configure > Convert to Maven project. The Create New POM dialog appears, enter the following information:
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> </dependencies>You can see these are two dependencies for Servlet API 3.1.0 and JSP API 2.3.1. Save the pom.xmlfile and Maven will download the dependent JAR files in few seconds, as you can see under the Libraries section of the project:

3. Creating a JSP Page
Right click on the project and select New > JSP File. Enter index.jspas the file name in the New JSP File dialog:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hello World Java EE</title> </head> <body> <h1>Hello JSP and Servlet!</h1> </body> </html>As you can see, this page simply uses HTML code to display the text “Hello JSP and Servlet!”.
4. Testing the JSP Page
Now, let’s test the JSP page to see if the server works normally. If you haven’t added Tomcat server to Eclipse, follow this tutorial to add Apache Tomcat to the IDE.To deploy the project on Tomcat, simply drag the project from the Project Explorer view to Servers view, which results in the following screenshot:
INFO: Server startup in 1239 msNow we can test the JSP page either by:- Right click on the project, select Run As > Run on Server and click Finish in the Run on Server dialog. This will open an internal web browser inside the IDE like this:

http://localhost:8080/HelloWorldJavaEE/
5. Update the JSP Page (Adding Form)
Next, let’s update the JSP page to work with a Java servlet. The idea is adding a web form and on submit, the form will be processed by the servlet. Add the following code between the<body> tags of the page:<h1>Hello JSP and Servlet!</h1> <form action="helloServlet" method="post"> Enter your name: <input type="text" name="yourName" size="20"> <input type="submit" value="Call Servlet" /> </form>This HTML code adds a form with a text field and a submit button to the page. The actionattribute of the form specifies the URL handles this form’s submission. In this case, we specify a path which is relative to the application and it points to a Java servlet which we will create in the next section.Save the file and refresh the browser, you will see:


6. Creating a Java Servlet
Before creating a servlet, let’s create a Java package named net.codejava.javaee by right click on the project, select New > Package. Enter that package name in the New Java Package dialog.Now click on the newly created package name and select New > Servlet, then enter HelloServlet as class name in the Create Servlet dialog:



String yourName = request.getParameter("yourName"); PrintWriter writer = response.getWriter(); writer.println("<h1>Hello " + yourName + "</h1>"); writer.close();This code simply retrieves value of the field yourNamefrom the form and writes a String to the response.
7. Testing the Servlet
Save the HelloServlet class and restart the server. Refresh the home page and enter your name in the form, for example:

Other Java Servlet Tutorials:
- Java Servlet Quick Start for beginners (XML)
- Java Servlet for beginners (annotations)
- Handling HTML form data with Java Servlet
- Java File Download Servlet Example
- Upload file to servlet without using HTML form
- How to use Cookies in Java web application
- How to use Session in Java web application
About the Author:

Comments
JDK 17
Eclipse 2021-12
Apache Tomcat 10