Programming on the Web(CSC309F) Tutorial: Servlets && Tomcat TA:Wael Aboelsaadat Acknowledgments : This tutorial is based on a series of articles written by James Goodwill about Tomcat && Servlets. 1
Tomcat What is it? Java based Web Application container that was created to run Servlets and JavaServer Pages (JSP) in Web applications. Installed within Apache or standalone. Client: browser Web Server: Apache Tomcat Web Applications 2
Tomcat: Configuration Server.xml Main configuration file. Exists in conf directory of your tomcat installation. Sample file: <Server port="8005" shutdown="shutdown" debug="0"> <Service name="tomcat-standalone"> <Connector classname="org.apache.catalina.connector.http.httpconnector" port="8080" minprocessors="5 maxprocessors="75" enablelookups="true" redirectport="8443" acceptcount="10" debug="0 connectiontimeout="60000"/> <Engine name="standalone" defaulthost="localhost" debug="0"> <Logger classname="org.apache.catalina.logger.filelogger" prefix="catalina_log." suffix=".txt" timestamp="true"/> <Realm classname="org.apache.catalina.realm.memoryrealm" /> <Host name="localhost" debug="0" appbase="webapps" unpackwars="true"> <Valve classname="org.apache.catalina.valves.accesslogvalve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common"/> <Logger classname="org.apache.catalina.logger.filelogger" directory="logs" prefix="localhost_log. suffix=".txt" timestamp="true"/> <Context path="/examples" docbase="examples" debug="0" reloadable="true"> <Logger classname="org.apache.catalina.logger.filelogger" prefix="localhost_examples_log." suffix=".txt" timestamp="true"/> </Context> </Host> </Engine> </Service> 3
Tomcat: Configuration server.xml Server.xml <Server>: Represents the entire Tomcat container. Used as a top-level element for a single Tomcat instance <Service>: Acts as a container for one or more <Connector> elements that share a single <Engine> element <Engine>: Represents the Catalina servlet container. There can only be one <Engine> element for each defined <Service>. This single <Engine> component will receive all requests received by all of the defined <Connector> components. The <Engine> element must be nested immediately after the <Connector> elements, inside of its owning <Service> element. <Host>: Defines the virtual hosts that are contained in each instance of a Catalina <Engine>. Each <Host> can be a parent to one or more Web applications, which are represented by a <Context> component <Context>: Represents an individual Web application that is running within a defined <Host>. There is no limit to the number of contexts that can be defined within a <Host> element. Each <Context> definition must have a unique context path, which is defined by the path attribute. <Connector>: Defines the component that does the actual managing of requests and responses to and from a calling client. 4
Tomcat: Deploying web applications War file War => web application archive ( how? jar cvf example.war. ) web.xml: <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app> <display-name>example App</display-name> <session-timeout>30</session-timeout> <servlet> <servlet-name>exampleservlet</servlet-name> <servlet-class>packagename.exampleservlet</servlet-class> <init-param> <param-name>parameter</param-name> <param-value>value</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> ExampleServlet </servlet-name> <url-pattern>/example</url-pattern> </servlet-mapping> </web-app> Invoking servlet: http://localhost:8080/servlet/example 5
Tomcat: Setup && Configuration Directories: /bin => contains the startup and shutdown scripts for both Windows and Linux. /conf => contains the main configuration files for Tomcat. The two most important are the server.xml and the global web.xml /server => contains the Tomcat Java Archive files /lib => contains Java Archive files that Tomcat is dependent upon /logs => contains Tomcat's log files. /src => contains the source code used by the Tomcat server /webapps => All web applications (including yours) are deployed in this directory; it contains the WAR file /work => This is the directory in which Tomcat will place all servlets that are generated from JSPs. If you want to see exactly how a particular JSP is interpreted, look in this directory. 6