JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
Creating and Deploying a Servlet on Tomcat
This section provides a tutorial example on how to create a simple Servlet class, compile it and deploy it on the Tomcat server.
To confirm that Apache Tomcat is a Servlet engine or container, I did the following steps to create my first Servlet page:
1. Write the following simple Servlet example in any text editor:
/* HelloServlet.java * Copyright (c) 2002 HerongYang.com. All Rights Reserved. */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<html><body>"); out.println("<p>Hello World! -- From Servlet</p>"); out.println("</body></html>"); out.close(); } }
2. Save this Servlet class file, HelloServlet.java, to the "classes" folder of the default application folder on the Tomcat server: \local\tomcat\webapps\ROOT\WEB-INF\classes\
3. Compile this Servlet class file into Java bytecode in a command window. Note that "servlet-api.jar" is needed to complete the compilation.
herong> set classpath=\local\tomcat\lib\servlet-api.jar herong> cd \local\tomcat\webapps\ROOT\WEB-INF\classes \local\tomcat\webapps\ROOT\WEB-INF\classes>javac HelloServlet.java \local\tomcat\webapps\ROOT\WEB-INF\classes>dir 791 HelloServlet.class 571 HelloServlet.java
4. Map the Servlet to a URL path name on the Tomcat server by editing the "\local\tomcat\webapps\ROOT\WEB-INF\web.xml" file:
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- ... --> <web-app 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_4_0.xsd" version="4.0" metadata-complete="true"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <!-- Add the following 8 lines to map a Servlet class to a URL --> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/Hello.servlet</url-pattern> </servlet-mapping> </web-app>
5. Access the Servlet class through the mapped URL "http://localhost:8080/Hello.servlet" with any browser. The output of my first Servlet class shows up:
Hello World! -- From Servlet
Congratulations! I have successfully created and deployed a Servlet class on the Tomcat server!
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
►Creating and Deploying a Servlet on Tomcat
Relationship between Servlet and JSP
Modifying the Servlet Converted from a JSP Page
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
Multiple Tags Working Together
Using Tomcat on CentOS Systems
Connecting to SQL Server from Servlet