Presentation is loading. Please wait.

Presentation is loading. Please wait.

Apache Tomcat Representation and Management of Data on the Web.

Similar presentations


Presentation on theme: "Apache Tomcat Representation and Management of Data on the Web."— Presentation transcript:

1 Apache Tomcat Representation and Management of Data on the Web

2 What is Tomcat? Tomcat is a Servlet container (Web server that interacts with Servlets) developed under the Jakarta Project at the Apache Software Foundation Tomcat implements the Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystems Tomcat is an open-source, non commercial project -Licensed under the Apache Software License Tomcat is written in Java (OS independent)

3 Tomcat Directory Structure

4 The directory TOMCAT-HOME contains executables and libraries required for the server launching, running and stopping -This directory is placed under /usr/local/… The directory TOMCAT-BASE contains the Web-site content, Web applications and configuration data -This directory is placed under your home directory

5 Installing Tomcat Create a directory for tomcat base -For example: mkdir ~/tomcat-base Set the environment variable CATALINA_BASE to your tomcat-base directory -For example: setenv CATALINA_BASE ~/tomcat-base -Insert this line into your.cshrc file Run ~dbi/tomcat/bin/setup $CATALINA_BASE is now a legitimate Tomcat base directory, and Tomcat is ready to run

6 Choosing a port for Tomcat In the file $CATALINA_HOME/conf/server.xml you will find the element Connector of the service Catalina Choose a port (greater than 1024) and change the value of the port attribute to your chosen one: … … …

7 Running Tomcat To start tomcat use ~dbi/tomcat/bin/catalina run Or, in background ~dbi/tomcat/bin/catalina start To stop tomcat use ~dbi/tomcat/bin/catalina stop To see the default page of Tomcat from your browser use the URL http:// : / -machine-name is the name of the machine on which Tomcat runs and port is the port you chose for Tomcat You can also use http://localhost: / if your browser runs on the same machine as Tomcat

8

9

10 Creating Web Applications A Web application is a self-contained subtree of the Web site. It has a distinct context, sessions, and Servlet mappings A Web application usually contains several Web resources like HTML files, Servlets and JSP files, and other resources like Database tables Each Web application has its own subdirectory under the directory $CATALINA_BASE/webapps/

11 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:// : /myApp/index.html http:// : /myApp/

12 The Directory Structure of a Web Application You can also use subdirectories under myApp For example: the file myApp/myImages/im.gif is mapped to by the URL http://machine:port/myApp/myImages/im.gif Tomcat maps the root directory ( http://localhost:8090/ ) to the directory webapps/ROOT/

13 The Directory Structure of a Web Application An application's directory must contain the following: -The directory myApp/WEB-INF/ -A legal web.xml file under myApp/WEB-INF/

14

15

16 Tomcat and Java Classes Tomcat uses Java classes you provide in order to run Servlets and JSP files -For example, the Servlets themselves! Tomcat 5.x initialization scripts ignore your environment CLASSPATH variable Classes are expected to be placed (or linked) at some predefined places in its directories

17 Java Class Locations Tomcat expects to find Java classes in class files (in a directory named classes ) and JAR files (in a directory named lib ) in the following places: TOMCAT-HOME/common/ - basic runtime classes. No need to touch this directory $CATALINA_BASE/shared/ -classes that are used by all the Web applications $CATALINA_BASE/webapps/myApp/WEB-INF/ -application specific classes (Servlets are typically here)

18 Java Class Locations

19 Classes Provided by DBI Course In order to provide the classes you need, like ORACLE, SAX and DOM related packages, the Tomcat-setup script links the directory $CATALINA_HOME/shared/lib/ to ~dbi/tomcat/shared/lib/, thus the latter packages are automatically known by your Tomcat server

20 We know how static resources (e.g HTML, images) are advertised using Tomcat In order to advertise a Servlet in Tomcat we have to do the following: -Put the class file in a proper place -Tell Tomcat that the class acts as a Servlet -Tell Tomcat the URL mapping of the Servlet 2 and 3 are discussed in the following slides Advertising a Servlet

21 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 -Session time-out specification -Context (application) parameters -Error pages (sent in cases of HTTP errors) -Security constraints, and more…

22 Servlet Declaration and Mapping The element declares a Servlet The sub element defines an initialization parameter to the Servlet -Access using ServletConfig.getInitParameter() The element maps a URL to a specific Servlet -The URL is relative to the application’s base URL

23 hi HelloWorld login snoopy hi /welcome Matches: http://localhost/myApp/welcome web.xml Example

24 hi *.welcome hi /welcome/* Matches: http://localhost/myApp/a.welcome http://localhost/myApp/sub/b.welcome More Mapping Examples Matches: http://localhost/myApp/welcome/a.html How can the Servlet know the original URL?

25 A Tip Tomcat provides a Servlet that enables invoking an existing Servlet class without declaration and mapping To enable this feature, uncomment the elements servlet and servlet-mapping in the file of the Servlet called invoker in $CATALINA_BASE/conf/web.xml To call the compiled Servlet myServlet.class in the application myApp use this URL: http:// : /myApp/servlet/myServlet NEVER publish a Web-site with this feature enabled! -Otherwise, your security restrictions are easily bypassed

26 More Configurations Set the session time-out value using under the element -This value is the maximal time of inactivity within a session, specified in minutes Set context parameters using -Access using ServletContext. getInitParameter() -Use this for parameters that are relevant for the whole application (the webmaster’s email, for example)

27 20 webmaster myaddress@company.com web.xml Example

28 Yet More Configurations Use the element to define the page sent in case of an HTTP error that occurs within the application context When the URL of the application base directory is requested, what page should be sent? You can define this property using the element

29 Functional Pages Example Welcome Welcome, dear client! myWelcome.html 404 Error WHAT????? my404.html

30 Functional Pages Example myWelcome.html index.html index.jsp 404 /my404.html

31

32

33

34

35 web.xml DTD Your web.xml file must conform to the web-app DTD: http://java.sun.com/dtd/web-app_2_3.dtd

36 Reflecting Application Changes Usually, you have to restart Tomcat after changing your Web application Why? -web.xml is processed at Tomcat startup -Some of the Java classes may have already been loaded and initialized, so changes will not be reflected There are more elegant solutions

37 Web Application Development

38 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: > jar cvf myApp.war /* 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 extension) -The WAR will not be unpacked if webapps/ already contains the unpacked directory and the WAR is not newer...

39 Tomcat 5.0 Manager Tomcat 5.0 comes with a Web application called “manager”, which supports functions for managing Web application with no need to restart tomcat You can either use the HTML interface at http:// : /html/ or send direct HTTP requests to its Servlets http://<machine>:<port>/html/ You will need to authenticate as a privileged user Use the username “admin” with no password

40

41 Tomcat 5.0 Manager Using the manager, you can -Deploy a Web application from either a WAR file (POST command) or an existing directory in the server’s webapps/ (GET command) -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 (if exists) from webapps/

42 Application Development with ANT Apache provides an ANT package for Tomcat Web- application development For example: - ant compile - compiles your java classes and creates the application WAR file - ant deploy - (re)deploys your application ANT actually interacts with the manager application Detailed explanations in the course home-page

43

44 Tomcat and Eclipse An additional option is to use an Eclipse plugin for Tomcat Web-application development The “Sysdeo Eclipse Tomcat Launcher” plugin is installed in CS This plugin, like ANT, interacts with Tomcat manager Detailed explanations in the course home-page

45

46 Web Development Overview Several options for Web development were presented: Work directly in the application's base directory -Restart Tomcat to reflect changes, or use the manager home page to reload the application Work in a separate directory and use ANT to compile, deploy and reload the application Manage your Web-application directory using Eclipse -Does edit, compile and reload of the application

47 Our Recommendation To better understand and control Tomcat, we suggest that the first steps in your application development will be taken in the direct approach (work directly on Tomcat’s files) Once you understand how Tomcat works, either copy your application's directory to a new location and use ANT, or start a Tomcat project in Eclipse, with your application's directory as the project base Our recommendations are OS independent!


Download ppt "Apache Tomcat Representation and Management of Data on the Web."

Similar presentations


Ads by Google