This assumes that you have set up Java, Maven, and Eclipse.
To run JEE based web apps, you need an application server that contains a web container. There are a number of application servers like Apache Tomcat, JBoss, GlassFish, etc. This tutorial will use Apache Tomcat.
Installing & configuring Tomcat
Step 1: Download and install Apache Tomcat from
http://tomcat.apache.org/ and then “Download” on the LHS menu and then select the version. Usually the latest version.
The installing is all about unzipping it to the right folder (e.g. c:\tools). If you are running on Windows 7 or greater download the 64-bit windows zip. For example, “Tomcat 9” for Windows 10 will be “64-bit Windows zip”
Step 2: Set up the “JRE_HOME” environment variable. E.g. c:\scripts\environment-setup.bat
1 2 3 4 5 6 7 8 9 | REM This is sample to set up your Java SET TOOLS_HOME=C:\tools SET JAVA_HOME=%TOOLS_HOME%\jdk-8u131-windows-x64 SET JRE_HOME=%TOOLS_HOME%\jdk-8u131-windows-x64\jre SET M3_HOME=%TOOLS_HOME%\apache-maven-3.5.0-bin\apache-maven-3.5.0 SET MAVEN_OPTS=-Xms1024m -Xmx1G SET PATH=%PATH%;%JAVA_HOME%\bin;%M3_HOME%\bin |
Step 3: Open a DOS console by typing “cmd” on windows 10 search. Run
1 | c:\scripts\environment-setup.bat |
Step 3: Run the tomcat server as shown below:
1 | c:\tools\apache-tomcat-9.0.0.M21-windows-x64\apache-tomcat-9.0.0.M21\bin>startup.bat |
Creating a simple web app using JEE
Step 1: Create a web project “simpleWeb” using Maven.
1 | c:\projects>mvn -B archetype:generate -DgroupId=com.mytutorial -DartifactId=simpleWeb -DarchetypeArtifactId=maven-archetype-webapp |
Step 2: Import c:\projects\simpleWeb as “Maven -> Existing Maven Projects”.
Step 3: Add the Servlet dependency to the pom.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simpleWeb</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>simpleWeb Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>simpleWeb</finalName> </build> </project> |
Step 4: Define a simple servlet class in a package com.mytutorial under src/main/java. You need to first create a folder named “java” under src/main. Then create the package folders com.mytutorial under the folder java. Create a Java class named SimpleServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.mytutorial; 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; public class SimpleServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<h1>Simple</h1>"); out.println("</body>"); out.println("</html>"); } } |
Step 5: The simpleWeb\src\main\webapp\WEB-INF\web.xml should be modified to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Simple web app</display-name> <servlet> <servlet-name>Simple Servlet</servlet-name> <servlet-class>com.mytutorial.SimpleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Simple Servlet</servlet-name> <url-pattern>/simple</url-pattern> </servlet-mapping> </web-app> |
Step 6: Build the war file target\simpleWeb.war by running the “mvn package” command in a DOS console
1 | c:\projects\simpleWeb>mvn clean package |
You could also run it inside eclipse: by selecting the project “simpleWeb”, and then right-click to get the context menu followed by “Run As” –> “Maven install”.
Deploy “simpleWeb.war” to Tomcat & run
Step 1: Copy the “simpleWeb.war” to the tomcat “C:\tools\apache-tomcat-9.0.0.M21-windows-x64\apache-tomcat-9.0.0.M21\webapps” folder.
Step 2: Open a web browser and type the url
http://localhost:8080/simpleWeb/simple