Take time to be very familiar with this page.

We will use this method of creating Web Applications throughout the course. A link to this reference will be on each course session page in the Additional Information area below the main content list.

1) Eclipse Java EE:
New Dynamic Web Project Wizard

In Eclipse Java EE Edition: File -> New -> Dynamic Web Project
or
File -> New -> Project… -> (Scroll to Web and expand) -> Dynamic Web Project
eclipse-choose-dynamic-web-project

(Click the image to Zoom and use Back to come back)

1.1) Fill in the Form

eclipse-dynamic-web-project-1

2) Eclipse Java EE Edition: Project Explorer

2.1) Create a web.xml file for XML configuration

eclipse-dynamic-web-project-2
Afterwards expand the WEB-INF folder to see the web.xml file.
eclipse-dynamic-web-project-3

2.2) Create a new package for our Servlet

eclipse-dynamic-web-project-4

2.3) HelloWorldServlet.java

File -> New -> Class
or
to pre-fill the package input
right click on the package name -> New -> Class
eclipse-dynamic-web-project-5

Fill in the form to have Eclipse make a default class file for us:
eclipse-dynamic-web-project-6
Result:
eclipse-dynamic-web-project-7
Our code:

package com.ogbrown.webapps.servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class HelloWorldServlet
 */
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorldServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.print("<!DOCTYPE html>n" +
              "<html>n" +
              "<head>n" +
              "<meta charset="ISO-8859-1">n" +
              "<title>");
        out.print("Web Apps Dev");
        out.println("</title>n" + 
              "</head>n" +
              "<body>n");
        out.println("<h1>Hello Web Apps World</h1>");
        out.print("</body>n" +
              "</html>");
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
 
}

Result:
eclipse-dynamic-web-project-8

2.3) web.xml

We need to configure our web application to recognized our new servlet. Open the generated web.xml:
eclipse-dynamic-web-project-9
Our XML configuration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>WebStarterProj</display-name>
 
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>
             com.ogbrown.webapps.servlets.HelloWorldServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>
 
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Result:
eclipse-dynamic-web-project-10

2.4) Make sure Eclipse will open our WebApp in Firefox

eclipse-dynamic-web-project-10b

2.5) SAVE ALL FILES

Many errors are because programmers haven’t saved their latest updates.

2.6) Run our WebApp on our Tomcat Server in Eclipse

This should open a Firefox web browser and show our page (Make sure Firefox is running already).
eclipse-dynamic-web-project-11
Choose our Tomcat Server:
eclipse-dynamic-web-project-12
Restart the Server if it’s already running:
eclipse-dynamic-web-project-13

2.7) Our Output in Firefox

eclipse-dynamic-web-project-14


99) 🙂 Alternative: Import Completed Project

Eclipse™ Java Project
WebStarterProj.zip
(Download to your downloads folder or another easily remembered folder.)