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