Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Running Web Applications with Tomcat CS 236607, Winter 2007/8.

Similar presentations


Presentation on theme: "1 Running Web Applications with Tomcat CS 236607, Winter 2007/8."— Presentation transcript:

1 1 Running Web Applications with Tomcat CS 236607, Winter 2007/8

2 2 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

3 3 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.

4 4 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… More features…

5 5 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

6 6 Deployment Descriptors An XML file web.xml describing mapping from URIs to application resources initialization parameters security constraints registration of listeners and filters

7 7 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

8 8 Tomcat Directory Structure (5.5) Tomcat-Base webappswork JAR files ROOTmyApp1myApp2 server.xml Tomcat-users.xm l WEB-INF libclasses web.xml bincommonlogsconf lib JAR Files

9 9 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

10 10 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.

11 11 Creating Web Applications A Web application usually contains several different 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

12 12 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.gif is mapped to by the URL http://host:port/myApp/myImages/im.gif

13 13 The Directory Structure of a Web Application – Cont. An application's directory should contain the following:  The directory WEB-INF/  A legal web.xml file under WEB-INF/ Minimal content of web.xml

14 14 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…

15 15 Servlet Declaration and Mapping The element declares a Servlet The sub element defines a parameter passed to the Servlet  Access using: ServletConfig.getInitParameter( String paramName) The element maps a URL to a specific Servlet  The URL is relative to the application’s base URL ( http://host:port/myApp/ )

16 16 Publishing a Servlet -An Example hello HelloWorld hello /hi web.xml http://localhost/myApp/hi The 2 blocks do not have to be adjacent within web.xml myApp/WEB-INF/classes/HelloWorld.class

17 17 web.xml DTD Your web.xml file must conform to the web-app DTD:

18 18 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

19 19 Error Page Example Not Found Sorry, no such file... 404 /my404.html web.xml my404.html

20 20 A non-existing resource

21 21 And now for a live show… Tomcat & Eclipse

22 22 Web Application Development

23 23 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...

24 24 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

25 25 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 WAR s) Warning: while “stop” makes an application unavailable, “undeploy” deletes the application directory and WAR file from webapps/

26 26 Tomcat and Eclipse As you saw in the example Tomcat is easily integrated into Eclipse. All you need to do is download Apache Tomcat (you may download from the course site) and create a new server in Eclipse. Follow the Eclipse instructions…

27 27 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


Download ppt "1 Running Web Applications with Tomcat CS 236607, Winter 2007/8."

Similar presentations


Ads by Google