- Written by Nam Ha Minh
- Last Updated on 07 August 2019 Print Email
- JDK 7
- Eclipse IDE for Java EE Developers (Juno 4.2)
- Tomcat 7.0
You can use newer versions of these software programs.
Table of Content:
The mission of Eclipse EE4J is to create standard APIs, implementations of those APIs, and technology compatibility kits for Java runtimes that enable development, deployment, and management of server-side and cloud-native applications. EE4J is based on the Java EE standards, and uses Java EE 8 as the baseline for creating new standards. Eclipse IDE for Java Developers 'Servers' doesn't exist. Therefore download correct Eclipse IDE. It is Eclipse IDE for Enterprise Java Developers (Eclipse IDE for Java EE Developers). This way is easy. Tools for Java developers creating Java EE and Web applications, including a Java IDE, tools for Java EE and JSF, Mylyn and others. Eclipse IDE for Java Developers: For developing Java SE applications, which is a subset of the Java EE Developer package. To start downloading the package, click on the link 32-bit or 64-bit, depending upon the bit version of your operating system.
1. Create Dynamic Web Project
2. Create Servlet class
3. Deploy the servlet
4. Run and test the servlet
1. Create Java Dynamic Web ProjectServlet 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 classWe 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: HelloServletThe 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!');HelloServlet: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.