You can use newer versions of these software programs.
Table of Content:
- 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.
- 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/HelloServletClick 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:- 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.