In this Java tutorial, we will help you understand the process of developing a Java web application (based on Servlet and JSP) from scratch using Eclipse IDE with Maven as the build system and Tomcat as the web server. This tutorial is helpful for those who are beginner in JSP, Servlet, Eclipse, Maven and Tomcat.
The following technologies and software are used in this tutorial:
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:
Click Next two times to go to this screen:
Here we can check the option Generate web.xml deployment descriptor to create web.xml file. But since Servlet 3.0 can be declared with annotations, we can choose not to create this XML file. Click Finish.
Eclipse creates the project that looks something like this:
This is a typical Java EE project. Next, we will add Maven support.
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:
Note that the Group Id will be used as the main package for our Java code. Now you see the pom.xml file created in the project. Open it in XML mode and put the following code between the and elements:
javax.servlet javax.servlet-api 3.1.0 provided javax.servlet.jsp javax.servlet.jsp-api 2.3.1 provided
You can see these are two dependencies for Servlet API 3.1.0 and JSP API 2.3.1. Save the pom.xml file and Maven will download the dependent JAR files in few seconds, as you can see under the Libraries section of the project:
Right click on the project and select New > JSP File. Enter index.jsp as the file name in the New JSP File dialog:
Click Finish and you see Eclipse created the index.jsp file under the WebContent directory. Update it with the following code:
As you can see, this page simply uses HTML code to display the text “Hello JSP and Servlet!”.Hello World Java EE Hello JSP and Servlet!
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:
Right click on the server and select Start. Tomcat will be in Started state after few seconds and you see the last line in the Console view is something like this:
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:
If you see the page displays “Hello JSP and Servlet!”, that means the web application is up and running successfully. Congratulations!
- Another way is opening an external browser e.g. Google Chrome and enter the following URL (as shown in the internal web browser):
http://localhost:8080/HelloWorldJavaEE/
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 tags of the page:
Hello JSP and Servlet!
This HTML code adds a form with a text field and a submit button to the page. The action attribute 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:
Try to enter something in the text field and click Call Servlet, we get a 404 error page like this:
Don’t worry. This is because we haven’t created any Java servlet to handle this request. We will do so in the next section.
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:
Click Next. In the next screen, edit the URL mapping from /HelloServlet to /helloServlet as shown in the following screenshot:
NOTE: this URL mapping must match the value specified for the action property of the form in the JSP page so the servlet can process request from the form.
In the next screen, uncheck the option doGet in order to generate only doPost() method to handle the form:
Click Finish to let Eclipse generates code for the HelloServlet class, as shown below:
The @WebServlet annotation placed before the class declaration specifies that this class is a Java servlet which is responsible to handle requests in the form of /helloServlet .
The doPost() method is where we put the code to process HTTP POST request sent to this servlet. This method accepts two parameters HttpServletRequest represents the request and HttpServletResponse represents the response. These request and response objects are created and injected by the Servlet container (Tomcat).
Now, put the following code inside the method doPost() :
String yourName = request.getParameter("yourName"); PrintWriter writer = response.getWriter(); writer.println("Hello " + yourName + "
"); writer.close();
This code simply retrieves value of the field yourName from the form and writes a String to the response.
Save the HelloServlet class and restart the server. Refresh the home page and enter your name in the form, for example:
Click the button Call Serlvet and you see this response:
Congratulations! Seeing this page means that the servlet has fulfilled the request and gave a response.
So far we have walked you through the process of developing a ‘Hello world’ Java web application based on JSP and Servlet technologies using Eclipse IDE with Maven as the build system and Tomcat as the server. We hope you found this tutorial helpful, and thanks for reading.
You can also follow this video tutorial (something might be different than the text in this article):
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
HelloWorldJavaEE.zip | [Java JSP and Servlet Hello World Eclipse project] | 13 kB |