This is a short introduction to help you set up Tomcat and deploy Java servlets. For more information, please refer to Apache Tomcat.
(Please tell me if you find any errors or omissions in the document —Upsorn Praphamontripong, 11-January-2018)
apache-tomcat
zip file
C:\apache-tomcat
Applications/apache-tomcat
C:\apache-tomcat\bin\startup.bat
, orC:\apache-tomcat\bin\
then
execute startup.bat
Applications/apache-tomcat/bin/
then
execute ./startup.sh
http://localhost:8080/
C:\apache-tomcat\bin\shutdown.bat
, orC:\apache-tomcat\bin\
then
execute shutdown.bat
Applications/apache-tomcat/bin/
then
execute ./shutdown.sh
Servers
tab.
Servers
tab does not appear on the screen, open the view using
menu bar Window > Show View > Servers
Servers
tab, right click and select New > Server
Finish
Start
.java
file and
select Run As > Run on Server
,
Eclipse will prompt a window to start the server.
You can also choose to have the server starts automatically as you run the servlet.
Stop
.java
file)
servlet-api.jar
while
some require that you set class path to servlet-api.jar
Create a new Java EE Web Project
or
using the menu bar File > New > Dynamic Web Project
Next
and Next
to configure Web Module
.
Note: You may choose to click Finish
and
generate web.xml
to configure Web Module
later.
Web Module
window, select
Generate web.xml deployment descriptor
.
Note: If you choose to manually create web.xml
later, you may leave the checkbox blank.
Java Resources
,
right click on the src
folder, select
New > Servlet
to create a new Java servlet (.java
file).
Class name
.
If you choose to have a package for your servlets, you may enter the package name in
Java package
.
New > Servlet
(above), select
New > Package
to create a package.
Then, right click on the newly created package,
select New > Servlet
to create a servlet.
servlet-api.jar
to your project.
servlet-api.jar
to your project
Properties
, or
Project > Properties
Java Build Path
Libraries
tabAdd External JARs...
button
servlet-api.jar
.
Note: By default, servlet-api.jar
is part of Apache Tomcat and
is usually in
C:\apache-tomcat\lib
Applications/apache-tomcat/lib
servlet-api.jar
Open
button.
servlet-api.jar
should appear under
JARs and class folders on the build path panel
doGet()
method as neededdoPost()
method as needed.java
file and thus must be compiled before it can be run.
.java
file and select
Run as > Run on Server
), Eclipse automatically compiles the file before running it.
servlet-api.jar
(presumably in apache-tomcat/lib/
folder).
Control Panel > System > Advanced > Environment Variables
.
javac -cp "C:\Applications\apache-tomcat\lib\servlet-api.jar" servlet-name.java
javac -classpath "/Applications/apache-tomcat/lib/servlet-api.jar" servlet-name.java
javac
, you need to compile the servlet from "above" the directory:
javac package-name/Hello.java
(assuming servlet-api.jar
has already been included in the classpath)
servlet.annotation.WebServlet
servlet.annotation.WebServlet
to your Java file by including the following line
import javax.servlet.annotation.WebServlet;
@WebServlet("/your-servlet-name")
web.xml
file
web.xml
file
under your project directory (so-called "context")
in apache-tomcat folder.
Then, add the <servlet>
and <servlet-mapping>
tags
to web.xml
file.
C:\apache-tomcat\your-project-name\WEB-INF\web.xml
Applications/apache-tomcat/your-project-name/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>YourServletName</servlet-name> <servlet-class>YourServletClass</servlet-class> </servlet> <servlet-mapping> <servlet-name>YourServletName</servlet-name> <url-pattern>/YourURLMapping</url-pattern> </servlet-mapping> </web-app>
HelloServlet.java
(HelloServlet.class
, after compiled):
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/Hello</url-pattern> </servlet-mapping> </web-app>With the above servlet mapping, to access the servlet,
http://localhost:8080/cs4640/Hello
.
QAselector
and
package name is jeopardy
<servlet> <servlet-name>QAselector</servlet-name> <servlet-class>jeopardy.QAselector</servlet-class> </servlet> <servlet-mapping> <servlet-name>QAselector</servlet-name> <url-pattern>/QAselector</url-pattern> </servlet-mapping>The URL would be
http://localhost:8080/cs4640/QAselector
.
<servlet> <servlet-name>QAselector</servlet-name> <servlet-class>jeopardy.QAselector</servlet-class> </servlet> <servlet-mapping> <servlet-name>QAselector</servlet-name> <url-pattern>/jeopardy.QAselector</url-pattern> </servlet-mapping>The URL would be
http://localhost:8080/cs4640/jeopardy.QAselector
.
.war
and .class
.
The rest of these instructions assume you are deploying a simple .class
file.
C:\apache-tomcat\webapps
Applications/apache-tomcat/webapps
webapps
folder.
Assume you give your project name as cs4640
:
Directory | Contains |
\cs4640 |
This is the root directory of the web application. All JSP and HTML files are stored here. |
\cs4640\WEB-INF\classes |
This directory is where servlet and utility classes are located. |
\cs4640\WEB-INF |
This directory contains all resources related to the application
that are not in the document root of the application.
This is where your web application deployment descriptor (web.xml ) is located.
Note that the WEB-INF directory is not part of the public document.
No files contained in this directory can be served directly
to a client. |
\cs4640\WEB-INF\lib |
This directory contains Java Archive (JAR) files that the web application depends upon. For example, this is where you would place a JAR file that contains a JDBC driver. |
classes
directory.
Note that when you replace a class in the classes
directory, the files do not get reloaded dynamically.
You must restart the server (shutdown tomcat, start it again).
.java
source files
in the public directory!
apache-tomcat/webapps/project-name/WEB-INF/classes/package-name
chmod 755 *
http://localhost:8080/project-name/servlet-name
.java
file), select Run As > Run on Server
.
Eclipse will access the servlet using a virtual browser.
apache-tomcat/logs/
.
The files catalina.out
and
local_access_log.2018-MM-DD.txt
accumulate log entries from Tomcat
(MM
is a 2-digit indicator of the month and
DD
is a 2-digit indicator of the day).
You can look here to find error messages from your servlets.
try { ... ... } catch (Exception e) { System.out.println(e.getMessage()); System.exit(0); }The servlet is running inside Tomcat.
System.exit(0);
will cause Tomcat to exit.
Everyone who shares the server with you will be very angry!