New to Tomcat? New to servlets? Verify how the pieces of a web app work together in Tomcat by creating a web project with an HTML web page and a servlet. In this example, all the coding takes place under the Tomcat folder which isn't good practice but, we're not concerned about this code. We want to know how things work! Because tools obscure how things work, do all the coding with Notepad and compile from the command prompt. (It won't kill you!)
Our application flows from a web page to a servlet which writes a new web page that will appear in the browser. Our web page will submit a form to Tomcat with the action of "cookingservlet" which is mapped in the web.xml file to the com.cookbook.CookingServlet class. Note the mapping in web.xml of <url-pattern>/cookingservlet</url-pattern> to the com.cookbook.CookingServlet class.
- Create a folder under Tomcat/webapps named "cookbook".
- Create a folder in Tomcat/webapps/cookbook named "WEB-INF".
- Create the following folders in WEB-INF -
classes
classes/com
classes/com/cookbook
- Create the web.xml file in webapps/cookbook/WEB-INF.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>CookingServlet</servlet-name>
<servlet-class>com.cookbook.CookingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookingServlet</servlet-name>
<url-pattern>/cookingservlet</url-pattern>
</servlet-mapping>
</web-app>
- Create the CookingServlet.java source in WEB-INF/classes/com/cookbook
package com.cookbook;
import javax.servlet.*;
import javax.servlet.http.*;
public class CookingServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
java.io.PrintWriter out = response.getWriter();
out.println( "<html><body>Sizzle!</body></html>" );
}
}
- Create the index.html file in Tomcat/webapps/cookbook
<html>
<body>
<form action="cookingservlet">
<input type="submit" value="Start Your Grills"/>
</form>
</body>
</html>
- Compile the Java from the WEB-INF/classes folder:
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\lib\servlet-api.jar
javac com/cookbook/*.java
- Start Tomcat if necessary.
- Open a browser to http://localhost:8080/cookbook/
- Click the button!
Another Example
pom.xml
<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>EzJSP</groupId>
<artifactId>ezjsp</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>EzJSP</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>ezjsp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8081</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/webapp/WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Hello Servlet</servlet-name>
<servlet-class>com.ezjsp.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello Servlet</servlet-name>
<url-pattern>helloServlet</url-pattern>
</servlet-mapping>
</web-app>
src/main/java/com/ezjsp/HelloServlet.java
package com.ezjsp;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
getServletConfig().getServletContext()
.getRequestDispatcher("/hello.jsp").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
src/main/webapp/index.jsp
<html>
<body>
<h2>Hello World!</h2>
<form method="POST" action="helloServlet">
<input value="HelloServlet" type="submit" />
</form>
</body>
</html>
</pre></div>
<div style="background:#000;color:#0F8"><pre><html>
<body>
<h1> I have been invoked by Servlet
</body>
</html>
src/main/webapp/hello.jsp
<html>
<body>
<h1> I have been invoked by a Servlet<</h1>
</body>
</html>