JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
My First Servlet Page on CentOS
This section provides a tutorial example on how to create the first Servlet Java class to test the Servlet support of the Tomcat Web server on CentOS systems.
To confirm that Apache Tomcat is a Servlet engine or container, I did the following steps to create my first Servlet page on my local CentOS system:
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. Compile this Servlet class file into Java bytecode. Note that "servlet-api.jar" is needed to complete the compilation.
# javac -cp /usr/share/tomcat/lib/tomcat-servlet-3.0-api.jar \ HelloServlet.java # ls -l HelloServlet.* -rw-r--r--. 1 root root 791 Jan 8 19:07 HelloServlet.class -rwxr-xr-x. 1 root root 549 Jan 8 19:06 HelloServlet.java
3. Create the "WEB-INF\classes" sub-directory under my application directory on the Tomcat server. Make sure that it has "read" permission for all users.
# cd /usr/share/tomcat/webapps/herong # mkdir WEB-INF # mkdir WEB-INF/classes
4. Save this Servlet bytecode file, HelloServlet.class, to the "WEB-INF\classes" sub-directory. Make sure that it has "read" permission for all users.
# cp HelloServlet.class /usr/share/tomcat/webapps/herong/WEB-INF/classes
4. Map the Servlet to a URL path name on the Tomcat server by creating the "/usr/share/tomcat/webapps/herong/WEB-INF/web.xml" file. Make sure that it has "read" permission for all users.
<web-app> <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/herong/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 on my CentOS system!
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
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
Download and Install Tomcat on CentOS
Start/Stop Tomcat Server on CentOS
►My First Servlet Page on CentOS
Connecting to SQL Server from Servlet