O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Web Applications and Deployment

4.121 visualizações

Publicada em

Short presentation about Java web applications and deploying them on Apache Tomcat

  • Seja o primeiro a comentar

Web Applications and Deployment

  1. 1. Java Web Applications and Deployment Svetlin Nakov Borislava Spasova
  2. 2. Contents <ul><li>Using Tomcat Web Application Server </li></ul><ul><ul><li>What is a Web Container? </li></ul></ul><ul><ul><li>What is Apache Tomcat? </li></ul></ul><ul><ul><li>Installing and Running Tomcat </li></ul></ul><ul><ul><li>Tomcat Directory Structure </li></ul></ul><ul><li>Java Web Applications </li></ul><ul><ul><li>Web Application Structure and WAR Files </li></ul></ul><ul><ul><li>The Deployment Descriptor ( web.xml ) </li></ul></ul><ul><ul><li>Mapping a Servlet to URL </li></ul></ul><ul><ul><li>Creating Web Applications and Deploying on Tomcat </li></ul></ul>
  3. 3. Using Tomcat Web Application Server
  4. 4. What is a Web Container? <ul><li>Web containers are Java server applications </li></ul><ul><ul><li>Provide an environment for execution of Java Web applications, servlets, JSP, etc. </li></ul></ul><ul><li>Web containers </li></ul><ul><ul><li>Maintain the life cycle of the servlets – call their doGet(…) and do Post (…) methods </li></ul></ul><ul><ul><li>Issue a thread for each request </li></ul></ul><ul><ul><li>Give the servlet the HTTP request and return its response to the client </li></ul></ul><ul><li>Apache Tomcat is an example of a Web container </li></ul>
  5. 5. What Is Tomcat? <ul><li>Apache Tomcat is free, open source Java Web Application Server </li></ul><ul><ul><li>Java EE Web container </li></ul></ul><ul><ul><li>Can run as standalone HTTP server or can be attached to another HTTP server </li></ul></ul><ul><li>Tomcat can host: </li></ul><ul><ul><li>Java Web applications </li></ul></ul><ul><ul><ul><li>Servlets, JSP, custom tags, JSF </li></ul></ul></ul><ul><ul><li>Web Services </li></ul></ul>
  6. 6. Web Container and Web Server Integration <ul><li>A Web container may be used to process HTTP requests by executing the service method on an HttpServlet object </li></ul>
  7. 7. Installing Tomcat <ul><li>Tomcat can be freely downloaded from its official Web site: http://tomcat.apache.org/ </li></ul><ul><li>Requirements </li></ul><ul><ul><li>Java 5 or later on Windows / Linux / Unix / etc. </li></ul></ul><ul><li>Distributions </li></ul><ul><ul><li>Windows executable installation package </li></ul></ul><ul><ul><ul><li>Runs as Windows service </li></ul></ul></ul><ul><ul><li>ZIP / GZip archive </li></ul></ul><ul><ul><ul><li>Manually started / stopped by a script </li></ul></ul></ul>
  8. 8. Running Tomcat <ul><li>When installed from the ZIP archive Tomcat can be started by a script </li></ul><ul><ul><li>JAVA_HOME environment variable must point to JDK 6 or later installation folder: </li></ul></ul><ul><ul><li>Avoid spaces in the paths! </li></ul></ul>bin/startup.bat set JAVA_HOME= C:Progra~1Javajdk1. 6 .0_ 23 rem This space in the path will cause problems! set JAVA_HOME=&quot;C:Program FilesJavajdk1. 6 .0_ 23&quot;
  9. 9. Tomcat Directory Structure (as in Tomcat 7.0.11) <ul><li>$CATALINA_HOME = <some_dir>/apache-tomcat- 7 . 0 . 1 1 </li></ul><ul><li>bin/ Binary executables and scripts </li></ul><ul><li>conf/ Configuration files </li></ul><ul><li>logs/ Destination directory for log files </li></ul><ul><li>temp/ Directory used by the JVM for temporary files </li></ul><ul><li>webapps / Contains all Web applications deployed on the Web Server Can be used to deploy applications </li></ul><ul><li>work/ Scratch directory used by Tomcat for holding temp files and directories </li></ul>
  10. 10. Java Web Applications Structure and Deployment
  11. 11. Java Web Applications <ul><li>Java Servlet specification defines a Web application as a collection of: </li></ul><ul><ul><li>HTML pages, JSP pages and others </li></ul></ul><ul><ul><li>Servlets and compiled Java classes </li></ul></ul><ul><ul><li>Resources (images, CSS, files, etc.) </li></ul></ul><ul><li>Web applications are bundled and can be executed in any Web container </li></ul><ul><ul><li>Can compile into a W eb AR chive File (.WAR file) </li></ul></ul>
  12. 12. Web Applications Structure <ul><li>Java Web applications should have the following directory structure: </li></ul><ul><li>webapp/ The application root directory. Contains the files, accessible from the Web: HTML, CSS, JSP, images, ... </li></ul><ul><li>WEB-INF/ Special folder for the Web application </li></ul><ul><li>lib/ Libraries (JAR files) required by the application (e.g. JDBC drivers) </li></ul><ul><li>classes/ Compiled Java classes required by the application (servlets, beans, etc.) </li></ul><ul><li>web.xml Special configuration file called &quot; Web application deployment descriptor &quot; </li></ul>
  13. 13. Example – Login / Logout Web Application Structure The root directory of the Web application Classes of the application (including servlets) Libraries of the application (e.g. JDBC drivers) Deployment descriptor (configuration file) Public accessible files (HTML, JSP, CSS, ...) Special directory
  14. 14. WAR Files <ul><li>Java Web applications are compiled and packed into WAR files </li></ul><ul><li>WAR files are JAR archives (ZIP files) that contain Java Web application </li></ul><ul><ul><li>Consists of all the files of the application </li></ul></ul><ul><ul><ul><li>HTML, JSP and other files </li></ul></ul></ul><ul><ul><ul><li>Classes and libraries ( WEB-INF/classes/ , WEB-INF/lib/ ) </li></ul></ul></ul><ul><ul><ul><li>Deployment descriptor ( WEB-INF/ web.xml ) </li></ul></ul></ul><ul><ul><li>Can be deployed on any Web container </li></ul></ul>
  15. 15. WAR Files – Example <ul><li>With the Tomcat distribution comes an example WAR file : sample.war </li></ul><ul><ul><li>CATALINA_HOMEwebappsdocsappdevsamplesample.war </li></ul></ul>
  16. 16. The Deployment Descriptor ( web.xml ) <ul><li>The file <my-web-app>WEBINFweb.xml is called “ Web application deployment descriptor ” </li></ul><ul><li>It is read when server deploys the application </li></ul><ul><ul><li>Many servers have &quot;hot deploy&quot; option </li></ul></ul><ul><li>Basic format of web.xml file: </li></ul><ul><li>Order of the elements within the <web-app> tag is important! </li></ul><web-app version=&quot;2.4&quot; xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;> ... </web-app>
  17. 17. What is Defined in The Deployment Descriptor? <ul><li>The deployment descriptor of the Web application ( web.xml ) can define: </li></ul><ul><ul><li>The application name and description </li></ul></ul><ul><ul><li>Servlet classes and mappings to URL </li></ul></ul><ul><ul><li>Servlet configuration (init) parameters </li></ul></ul><ul><ul><li>Servlet filters definitions and filter mappings </li></ul></ul><ul><ul><li>Application context parameters </li></ul></ul><ul><ul><li>Welcome files, error pages, MIME mappings </li></ul></ul><ul><ul><li>Tag libraries references </li></ul></ul><ul><ul><li>Security and authentication settings </li></ul></ul>
  18. 18. Mapping a Servlet to URL <ul><li>Specifying servlet mappings in web.xml </li></ul><ul><ul><li>Giving a name to the servlet class </li></ul></ul><ul><ul><li>Mapping the servlet to URL or URL pattern </li></ul></ul><servlet> <servlet-name> LoginServlet </servlet-name> <servlet-class> com.mycompany.myproduct. web. LoginServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> LoginServlet </servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
  19. 19. <ul><li>Adding initialization parameters to servlets </li></ul><ul><li>Accessing the servlet initialization parameters </li></ul>Configuring The Servlet Initialization Parameters <servlet> <servlet-name>InitTest</servlet-name> <servlet-class>myservlets.InitServlet</servlet-class> <init-param> <param-name> username </param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>emailAddress</param-name> <param-value>admin@localhost</param-value> </init-param> </servlet> getServletConfig().getInitParameter(&quot; username &quot;);
  20. 20. Sample web.xml File <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <web-app version=&quot;2.4&quot; xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;> <servlet> <servlet-name>TimeServlet</servlet-name> <servlet-class>TimeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TimeServlet</servlet-name> <url-pattern>/TimeServlet</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
  21. 21. Creating Web Application and Deploying on Tomcat <ul><li>Create a new directory myapp in CATALINA_HOMEwebapps </li></ul><ul><li>Create subdirectories WEB-INF , WEB-INF/classes and WEB-INF/lib </li></ul><ul><li>Compile the HelloServlet.java and copy the class file HelloServlet.class to WEB-INF/classes/ </li></ul><ul><li>Copy the file HelloForm.html to the application root directory myapp / </li></ul><ul><li>Create the file deployment descriptor file WEB-INF/web.xml </li></ul>
  22. 22. Creating Web Application and Deploying on Tomcat (2) <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <web-app version=&quot;2.4&quot; xmlns= &quot;http://java.sun.com/xml/ns/j2ee&quot; xmlns:xsi= &quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;> <servlet> <servlet-name> Hello Servlet</servlet-name> <servlet-class> Hello Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name> Hello Servlet</servlet-name> <url-pattern>/ Hello Servlet</url-pattern> </servlet-mapping> </web-app> web.xml
  23. 23. Creating Web Application and Deploying on Tomcat (3) <ul><li>Using WinZip or normal jar-tool, wrap the complete Web application into a portable Web archive ( myapp.war ) </li></ul><ul><li>Delete the directory myapp (leave only the file myapp.war in TOMCAT_HOME/webapps ) </li></ul><ul><li>Start Tomcat and ensure that the application has been deployed (look at the console logs) </li></ul><ul><li>Browse the deployed Web application: </li></ul>http://localhost:8080/myapp/HelloForm.html
  24. 24. Problems <ul><li>Download and install Tomcat (use the ZIP distribution, not the Windows executable). </li></ul><ul><li>Deploy and r un the sample application from: CATALINA_HOMEwebappsdocsappdevsamplesample.war </li></ul><ul><li>Manually, without using Eclipse IDE, create a simple Java Web application consisting of a servlet that displays all the headers from the HTTP request. Map it to the URL pattern *.php . Create WAR archive with the application and deploy it on Tomcat. Try to browse the resource / index.php . </li></ul>
  25. 25. Homework <ul><li>Download and install Tomcat at home. Use the ZIP distribution, not the Windows executable. </li></ul><ul><li>Manually, without using any IDE, create a simple Web application that consists of a HTML form for entering a number and a servlet that calculates a square root of the number. Pack the application as WAR archive and deploy it on Tomcat. </li></ul><ul><li>Find information in Google about servlet filters . Add a servlet filter to the application that show current date and time on each page. </li></ul>