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 (Click the image to Zoom and use Back to come back) 1.1) Fill in the Form 2) Eclipse Java EE Edition: Project Explorer 2.1) Create a web.xml file for XML configuration Afterwards expand the WEB-INF folder to see the web.xml file. 2.2) Create a new package for our Servlet 2.3) HelloWorldServlet.java File -> New -> Class or to pre-fill the package input right click on the package name -> New -> Class Fill in the form to have Eclipse make a default class file for us: Result: 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: 2.3) web.xml We need to configure our web application to recognized our new servlet. Open the generated web.xml: 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: 2.4) Make sure Eclipse will open our WebApp in Firefox 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). Choose our Tomcat Server: Restart the Server if it’s already running: 2.7) Our Output in Firefox 99) 🙂 Alternative: Import Completed Project Eclipse™ Java ProjectWebStarterProj.zip (Download to your downloads folder or another easily remembered folder.) Page navigation ↑ Java Web Application Development ← JWAD: Install Tomcat Server JWAD: Homework JW1: Practice Environment →