How to create, deploy and run Java Servlet in Eclipse
- Details
- Written by Nam Ha Minh
- Last Updated on 07 August 2019   |   Print Email
In this tutorial, you will learn how to develop a simple Java Servlet using Eclipse IDE. Before moving forward, make sure you have the following pieces of software installed on your computer (clicking on the hyperlink will let you download the corresponding software):
You can use newer versions of these software programs.
Table of Content:
1. Create Java Dynamic Web Project
Servlet is for Java web application, so we need to create a Java EE project first. In Eclipse, make sure the current perspective is Java EE (if not, select Window > Open Perpsective > Java EE, or hold down Ctrl + F8 and select Java EE). Click File > New > Dynamic Web Project, the New Dynamic Web Project appears, enter the following information:
- Project name: MyFirstServlet
- Target runtime: Apache Tomcat v7.0. If Apache Tomcat v7.0 is not available in the dropdown list, click New Runtime button. The dialog New Server Runtime Environment appears, select Apache Tomcat v7.0:
Click Next. In the next screen, click Browse button to select Tomcat installation directory:
Click Finish and Apache Tomcat v7.0 will be added to the dropdown list.
- Dynamic web module version: 3.0
- Configuration: Default Configuration for Apache Tomcat v7.0
The configuration we have chosen conforms to Servlet specification version 3.0.
Leave other stuff as default, click Finish. The MyFirstServlet project is created.
2. Create Java Servlet class
We will create a dead simple servlet class which sends the output “Hello friend!” to client.
Click File > New > Servlet, the dialog Create Servlet appears. Enter the following information:
- Java package: net.codejava
- Class name: HelloServlet
The servlet being created has fully qualified name as net.codejava.HelloServletand extends HttpServlet class. Click Next to move on next screen where we can specify initialization parameters and URL mappings for the servlet:
Leave this form as default. The URL mapping /HelloServlet means that this servlet will be accessible from this URL pattern: http://localhost:8080/MyFirstServlet/HelloServlet
Click Next to move on next screen where we can specify which methods we want to inherit from the super class HttpServlet:
By default the two methods doGet()and doPost() are checked. Since we just query the servlet from a HTTP GET method, uncheck the doPost() method. Click Finish, Eclipse creates a Java class which has some skeleton code as below:
From this skeleton code, we can read out some interesting information as follow:
- The annotation @WebServlettells that the annotated class is a servlet. This annotation has been introduced since Servlet API 3.0. The URL mapping /HelloServlet is passed as a parameter of the annotation.
- HttpServlet class is the base class for all servlets that are serving client’s requests through HTTP.
- The doGet() method is overridden from the HttpServlet class, since we have chosen to override it in the dialog Create Servlet.
- The no-argument constructor HelloServlet() is generated to place initialization code. Since we don’t initialize anything, this constructor can be safely deleted.
Add the following line into the doGet() method:
response.getWriter().println("Hello friend!");
So far we have the following code of the servlet HelloServlet:
package net.codejava; 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 HelloServlet */ @WebServlet("/HelloServlet") public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello friend!"); } }
The doGet() method just simply sends a String “Hello friend!” to the client.
And finally, we have the following structure in the Project Explorer view:
Note that there is no file web.xml is created because that file can be omitted when we are using annotation for Servlet 3.0.
3. Deploy the Java Servlet
We will deploy the MyFirstServlet application on Tomcat server. Switch to the Servers view by selecting Window > Show View > Servers from Eclipse’s main menu.
If there is no server available, click on the link new server wizard…The New Server dialog appears, select Tomcat v7.0 Server as shown in the following screenshot:
Click Next. In the Add and Remove screen, select MyFirstServlet on the left and click Add button to move it to the right, as shown in the following screenshot:
Click Finish, the MyFirstServlet application is now deployed on Tomcat server:
4. Run and test the Java servlet
Now, it’s time to start the server and test the servlet. In the Servers view, right click on the server name, and select Start. You should see some output in the Console view, and the server’s status switches from Stopped to Started:
Click Window > Show View > Other…. In the dialog Show View, type “Internal” in the text field then select Internal Web Browser underneath:
Click OK to open up the view Internal Web Browser. Type the following URL into the address bar (the port number here may be different than yours, depending on how you configured Tomcat):
http://localhost:8080/MyFirstServlet/HelloServlet
The servlet is invoked and we would see the text “Hello friend!” in the browser:
That’s all for this tutorial! We have learnt how to develop a simple servlet – a basic step in developing Java EE applications.
Related Java Servlet Tutorials:
- Java Servlet tutorial for beginners (XML)
- Java Servlet tutorial for beginners (annotations)
- Java Servlet and JSP Hello World Tutorial with Eclipse, Maven and Apache Tomcat
Other Eclipse Tutorials:
- How to use Eclipse IDE for Java EE Developers
- How to create, build and run a Java Hello World program with Eclipse
- How to generate Javadoc in Eclipse
- How to generate JAR file in Eclipse
- How to create WAR file for Java web application in Eclipse
- How to create Ant build file for existing Java project in Eclipse
- How to pass arguments when running a Java program in Eclipse
- How to create Java web project with Maven in Eclipse
- 25 Eclipse Shortcut Keys for Code Editing
- How to Add Copyright License Header for Java Source Files in Eclipse
Comments
I found it, very clear and straight forward. thanks!
in Eclipsee