JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
My First Servlet Page on macOS
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 macOS 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 macOS 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.
herong$ javac -cp /Library/Tomcat/apache-tomcat-9.0.26/lib/servlet-api.jar \ HelloServlet.java herong$ ls -l HelloServlet.* -rw-r--r-- 1 herong staff 791 Jan 8 21:34 HelloServlet.class -rwxr-xr-x 1 herong staff 549 Jan 8 21:26 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.
herong$ cd /Library/Tomcat/apache-tomcat-9.0.26/webapps/herong herong$ mkdir WEB-INF herong$ 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.
herong$ cp HelloServlet.class \ /Library/Tomcat/apache-tomcat-9.0.26/webapps/herong/WEB-INF/classes
4. Map the Servlet to a URL path name on the Tomcat server by creating the "/Library/Tomcat/apache-tomcat-9.0.26/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 macOS 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
►Using Tomcat on macOS Systems
Download and Install Tomcat on macOS
Start/Stop Tomcat Server on macOS
►My First Servlet Page on macOS
Connecting to SQL Server from Servlet