270 likes | 288 Views
Running Web Applications with Tomcat. CS 236607, Spring 2008/9. Web Servers. The term web server can mean one of two things:
E N D
Running Web Applications with Tomcat CS 236607, Spring 2008/9
Web Servers The term web server can mean one of two things: • A computer program that is responsible for accepting HTTP requests from clients (browsers), and serving them HTTP responses along with optional data contents, which usually are web pages such as HTML documents and linked objects (images, etc.). • A computer that runs a computer program as described above. Web Servers rack
Common Features Although web server programs differ in detail, they all share some basic common features. • HTTP: every web server program operates by accepting HTTP requests from the client, and providing an HTTP response to the client. • Logging: usually web servers have also the capability of logging some detailed information, about client requests and server responses, to log files; this allows the webmaster to collect statistics by running log analyzers on log files. This NeXT workstation (a NeXTcube) was used by Tim Berners-Lee as the first Web server on the World Wide Web(1990). Today, it is kept in Microcosm, the public museum at the Meyrin site of CERN, in the Canton of Geneva, Switzerland. The document resting on the keyboard is a copy of "Information Management: A Proposal," which was Berners-Lee's original proposal for the World Wide Web.
More features… In practice many web servers implement the following features also: • Authentication, • Handling of not only static content but of dynamic content too by supporting one or more related interfaces (SSI, CGI, SCGI, FastCGI, JSP, PHP, ASP, ASP .NET, Server API such as NSAPI, ISAPI, etc.). • HTTPS support (by SSL or TLS) to allow secure (encrypted) connections to the server on the standard port 443 instead of usual port 80. • Content compression (i.e. by gzip encoding) to reduce the size of the responses (to lower bandwidth usage, etc.). • Virtual hosting to serve many web sites using one IP address. • Large file support to be able to serve files whose size is greater than 2 GB on 32 bit OS. • And more…
Web Applications Structure A Web app is structured as a directory: • myapp/– contains HTML/CSS/GIF/... files • myapp/WEB-INF/– contains the deployment descriptor web.xml • myapp/WEB-INF/classes/– contains servlet class files (in subdirs corresponding to package names) • myapp/WEB-INF/lib/– contains extra jar files
Deployment Descriptors An XML file web.xml describing • mapping from URIs to application resources • initialization parameters • security constraints • registration of listeners and filters
Apache Tomcat • Tomcat is a Servlet container (Web server that interacts with Servlets) developed under the Jakarta Project of Apache Software Foundation • Tomcat implements the Servlet and the Java Server Pages (JSP) specifications of Sun Microsystems • Tomcat is an open-source, non commercial project • Licensed under the Apache Software License • Tomcat is written in Java (OS independent) Comparison of web server software
Tomcat Directory Structure (5.5) Tomcat-Base conf logs webapps bin work common lib ROOT myApp1 myApp2 server.xml Tomcat-users.xml JAR files WEB-INF web.xml lib classes JAR Files
The Tomcat Server • lib/servlet-api.jar • The main jar file needed for compiling servlets. • bin/startup.sh, bin/shutdown.sh • Scripts for starting & stopping the server • conf/ • server.xml - Server configuration file: HTTP Port (Default 8080), SSL support… • Tomcat-users.xml – users definitions • webapps/myapp • The root for myapp application directory
Catalina • Catalina is the name of the servlet container of Apache Tomcat since version 4.x. • A Servlet container is a specialized web server that supports Servlet execution. It combines the basic functionality of a web server with certain Java/Servlet specific optimizations and extensions – such as an integrated Java runtime environment. Servlet containers are also referred to as web containers or web engines.
Creating Web Applications • A Web application usually containsseveraldifferent types of Web resources like HTML files, Servlets, JSP files, and other resources like Database tables • Each Web application has its own subdirectory under the directory $CATALINA_BASE/webapps/ • $CATALINA_BASE is an environment variable set to your tomcat-base directory (The directory that contains the Web-site content, Web applications and configuration data)
The Directory Structure of a Web Application • Tomcat automatically identifies a directory $CATALINA_BASE/webapps/myApp/ with the relative URL /myApp/ • For example, a file named index.html in myApp is mapped to by the following URLs: http://machine:port/myApp/index.html http://machine:port/myApp/ • You can also use subdirectories under myApp. For example: the file myApp/myImages/im.gifis mapped to by the URL http://host:port/myApp/myImages/im.gif
The Directory Structure of a Web Application – Cont. • An application's directory should contain the following: • The directory WEB-INF/ • A legalweb.xmlfile underWEB-INF/ Minimal content of web.xml <web-app> </web-app>
Configuring a Web Application • Application-specific configuration and declarations are written in the file myApp/WEB-INF/web.xml • This file contains: • Servlet declarations, mappings and parameters • Default files for directory requests (e.g index.html) • Error pages (sent in cases of HTTP errors) • Security constraints • Session time-out specification • Context (application) parameters • And more…
Servlet Declaration and Mapping • The element <servlet> declares a Servlet • The sub element <init-param> defines a parameter passed to the Servlet • Access using:ServletConfig.getInitParameter( String paramName) • The element <servlet-mapping> maps a URL to a specific Servlet • The URL is relative to the application’s base URL (http://host:port/myApp/)
myApp/WEB-INF/classes/HelloWorld.class <web-app> <servlet> <servlet-name>hello</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hi</url-pattern> </servlet-mapping> </web-app> web.xml Publishing a Servlet -An Example The 2 blocks do not have to be adjacent within web.xml http://localhost/myApp/hi
web.xml DTD Your web.xml file must conform to the web-app DTD: <!ELEMENTweb-app(icon?, display-name?, description?, distributable?, context-param*, filter*, filter-mapping*, listener*,servlet*, servlet-mapping*, session-config?, mime-mapping*,welcome-file-list?, error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*, login-config?, security-role*, env-entry*, ejb-ref*, ejb-local-ref*)>
Error Pages • Use the error-page element to define the page sent in case of an HTTP error that occurs within the application context • An error page element has two sub elements: • error-code - the HTTP error status code • location - the page that should be sent
<web-app> <error-page> <error-code>404</error-code> <location>/my404.html</location> </error-page> </web-app> web.xml Error Page Example <html> <head><title>Not Found</title></head> <body> <h1style="text-align:center; color:green"> Sorry, no such file... </h1> </body> </html> my404.html
Web Archives • A WAR (Web ARchive) file is a JAR file that contains a whole Web-application directory • For example, to create a WAR file of myApp do:cd webapps/myApp jar cvf myApp.war * (don’t forget the filename! What would happen otherwise?) • Tomcat unpacks all WAR files found in $CATALINE_BASE/webapps/at statup • The unpacked directory and context will be named as the WAR file name (without the .war extension) • The WAR will not be unpacked if webapps/ already contains the directory and the WAR is not newer...
Reflecting Application Changes • Changes in your Java classes may not be reflected in your application • Old versions may already have been loaded • The application needs to be reloaded • Changes in other files like HTML or JSP are always reflected • Modification of web.xml automatically causes the application to be reloaded
Tomcat 5.5 Manager • Tomcat 5.5 comes with a Web application called “manager”, which supports functions for managing Web applications • Using the manager, you can • Deploy a Web application by posting a WAR file • Undeploy a deployed Web application • Start/stop a Web application (make it available/unavailable) • Reload an existing Web application (unpack new WARs) • Warning:while “stop” makes an application unavailable, “undeploy”deletes the application directory and WAR file from webapps/
Tomcat and Eclipse • As you will see in the coming demo Tomcat is easily integrated into Eclipse. • All you need to do is download Apache Tomcat and create a new server in Eclipse (May also be done via Eclipse). • Follow the Eclipse instructions…
And now for a live demo… Tomcat & Eclipse
Resources • The Apache Software Foundation • An Introduction to XML and Web Technologies / Anders Møller and Michael I. Schwartzbach – course literature • Wikipedia • http://www.cs.huji.ac.il/~dbi • Sun Microsystems site