1 / 27

Running Web Applications with Tomcat

Running Web Applications with Tomcat. CS 236369, Spring 2010. Web Servers. The term web server can mean one of two things:

Download Presentation

Running Web Applications with Tomcat

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Running Web Applications with Tomcat CS 236369, Spring 2010

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

  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. Deployment Descriptors An XML file web.xml describing • mapping from URIs to application resources • initialization parameters • security constraints • registration of listeners and filters

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

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

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

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

  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. 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/)

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

  17. 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*)>

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

  20. A non-existing resource

  21. Web Application Development

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

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

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

  25. 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…

  26. And now for a live demo… Tomcat & Eclipse

  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

More Related