SlideShare a Scribd company logo
Java Web Applications   and Deployment Svetlin Nakov Borislava Spasova
Contents Using Tomcat Web Application Server What is a Web Container? What is Apache Tomcat? Installing and Running Tomcat Tomcat Directory Structure 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  do Post (…)  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 . 1 1 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 for holding temp files and directories
Java Web Applications Structure 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  W eb  AR chive  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 by the application (servlets, beans, etc.) web.xml Special configuration file called &quot; Web  application deployment descriptor &quot;
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
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 &quot;hot deploy&quot; option Basic format of  web.xml  file: Order of the elements within the  <web-app>  tag is important! <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>
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-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>
Adding initialization parameters to servlets Accessing the servlet initialization parameters 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;);
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>
Creating Web Application and Deploying on Tomcat Create a new directory  myapp  in  CATALINA_HOME\webapps Create subdirectories  WEB-INF ,  WEB-INF/classes  and  WEB-INF/lib Compile the  HelloServlet.java  and copy the class file  HelloServlet.class  to  WEB-INF/classes/ Copy the file  HelloForm.html  to the application root directory  myapp / Create the file deployment descriptor file  WEB-INF/web.xml
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
Creating Web Application and Deploying on Tomcat (3) Using WinZip or normal jar-tool, wrap the complete Web application into a portable Web archive ( myapp.war ) Delete the directory  myapp  (leave only the file  myapp.war  in  TOMCAT_HOME/webapps ) Start Tomcat and ensure that the application has been deployed (look at the console logs) Browse the deployed Web application: http://localhost:8080/myapp/HelloForm.html
Problems Download and install Tomcat (use the ZIP distribution, not the Windows executable). Deploy and r un  the  sample application from:  CATALINA_HOME\webapps\docs\appdev\sample\sample.war 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 Download and install Tomcat at home. Use the ZIP distribution, not the Windows executable. 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. Find information in Google about  servlet filters . Add a servlet filter to the application that show current date and time on each page.
Ad

More Related Content

What's hot (20)

Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 
From on premises monolith to cloud microservices
From on premises monolith to cloud microservicesFrom on premises monolith to cloud microservices
From on premises monolith to cloud microservices
Albert Lombarte
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
Arvind Devaraj
 
CQRS: Command/Query Responsibility Segregation
CQRS: Command/Query Responsibility SegregationCQRS: Command/Query Responsibility Segregation
CQRS: Command/Query Responsibility Segregation
Brian Ritchie
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
habib_786
 
What is load balancer in aws and types of load balancers
What is load balancer in aws and  types of load balancersWhat is load balancer in aws and  types of load balancers
What is load balancer in aws and types of load balancers
VishnuAnji
 
Odi 12c-getting-started-guide-2032250
Odi 12c-getting-started-guide-2032250Odi 12c-getting-started-guide-2032250
Odi 12c-getting-started-guide-2032250
Udaykumar Sarana
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Express JS Rest API Tutorial
Express JS Rest API TutorialExpress JS Rest API Tutorial
Express JS Rest API Tutorial
Simplilearn
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
Web Service Presentation
Web Service PresentationWeb Service Presentation
Web Service Presentation
guest0df6b0
 
MVC - Introduction
MVC - IntroductionMVC - Introduction
MVC - Introduction
Sudhakar Sharma
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
ivpol
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
Prem Sanil
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 
Laravel Tutorial PPT
Laravel Tutorial PPTLaravel Tutorial PPT
Laravel Tutorial PPT
Piyush Aggarwal
 
React JS
React JSReact JS
React JS
Software Infrastructure
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
Santosh Kumar Kar
 
Asp net
Asp netAsp net
Asp net
Dr. C.V. Suresh Babu
 
Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
Dmitry Buzdin
 
From on premises monolith to cloud microservices
From on premises monolith to cloud microservicesFrom on premises monolith to cloud microservices
From on premises monolith to cloud microservices
Albert Lombarte
 
CQRS: Command/Query Responsibility Segregation
CQRS: Command/Query Responsibility SegregationCQRS: Command/Query Responsibility Segregation
CQRS: Command/Query Responsibility Segregation
Brian Ritchie
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
habib_786
 
What is load balancer in aws and types of load balancers
What is load balancer in aws and  types of load balancersWhat is load balancer in aws and  types of load balancers
What is load balancer in aws and types of load balancers
VishnuAnji
 
Odi 12c-getting-started-guide-2032250
Odi 12c-getting-started-guide-2032250Odi 12c-getting-started-guide-2032250
Odi 12c-getting-started-guide-2032250
Udaykumar Sarana
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Express JS Rest API Tutorial
Express JS Rest API TutorialExpress JS Rest API Tutorial
Express JS Rest API Tutorial
Simplilearn
 
Web Service Presentation
Web Service PresentationWeb Service Presentation
Web Service Presentation
guest0df6b0
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
ivpol
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
Prem Sanil
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 

Viewers also liked (20)

WebLogic Deployment Plan Example
WebLogic Deployment Plan ExampleWebLogic Deployment Plan Example
WebLogic Deployment Plan Example
James Bayer
 
ZK_Arch_notes_20081121
ZK_Arch_notes_20081121ZK_Arch_notes_20081121
ZK_Arch_notes_20081121
WANGCHOU LU
 
3) web development
3) web development3) web development
3) web development
techbed
 
Introduction to jsf 2
Introduction to jsf 2Introduction to jsf 2
Introduction to jsf 2
yousry ibrahim
 
7) packaging and deployment
7) packaging and deployment7) packaging and deployment
7) packaging and deployment
techbed
 
JSF 2.2
JSF 2.2JSF 2.2
JSF 2.2
Edward Burns
 
Rich faces
Rich facesRich faces
Rich faces
BG Java EE Course
 
Lecture 11 bitwise_operator
Lecture 11 bitwise_operatorLecture 11 bitwise_operator
Lecture 11 bitwise_operator
eShikshak
 
principle of oop’s in cpp
principle of oop’s in cppprinciple of oop’s in cpp
principle of oop’s in cpp
gourav kottawar
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
Raghuveer Guthikonda
 
Java Server Faces (JSF) - advanced
Java Server Faces (JSF) - advancedJava Server Faces (JSF) - advanced
Java Server Faces (JSF) - advanced
BG Java EE Course
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
vikram singh
 
Screenshot of Desktop System Application Complete
Screenshot of Desktop System Application CompleteScreenshot of Desktop System Application Complete
Screenshot of Desktop System Application Complete
Hendrawan Hendrawan
 
12 installing eclipse
12   installing eclipse12   installing eclipse
12 installing eclipse
Zeeshan-Shaikh
 
Linux directory structure by jitu mistry
Linux directory structure by jitu mistryLinux directory structure by jitu mistry
Linux directory structure by jitu mistry
JITU MISTRY
 
Joget Workflow v5 Training Slides - Module 16 - Preparing Development Environ...
Joget Workflow v5 Training Slides - Module 16 - Preparing Development Environ...Joget Workflow v5 Training Slides - Module 16 - Preparing Development Environ...
Joget Workflow v5 Training Slides - Module 16 - Preparing Development Environ...
Joget Workflow
 
WebService-Java
WebService-JavaWebService-Java
WebService-Java
halwal
 
Linux Directory Structure
Linux Directory StructureLinux Directory Structure
Linux Directory Structure
Kevin OBrien
 
Java Server Faces (JSF) - Basics
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
BG Java EE Course
 
3 java - variable type
3  java - variable type3  java - variable type
3 java - variable type
vinay arora
 
WebLogic Deployment Plan Example
WebLogic Deployment Plan ExampleWebLogic Deployment Plan Example
WebLogic Deployment Plan Example
James Bayer
 
ZK_Arch_notes_20081121
ZK_Arch_notes_20081121ZK_Arch_notes_20081121
ZK_Arch_notes_20081121
WANGCHOU LU
 
3) web development
3) web development3) web development
3) web development
techbed
 
7) packaging and deployment
7) packaging and deployment7) packaging and deployment
7) packaging and deployment
techbed
 
Lecture 11 bitwise_operator
Lecture 11 bitwise_operatorLecture 11 bitwise_operator
Lecture 11 bitwise_operator
eShikshak
 
principle of oop’s in cpp
principle of oop’s in cppprinciple of oop’s in cpp
principle of oop’s in cpp
gourav kottawar
 
Java Server Faces (JSF) - advanced
Java Server Faces (JSF) - advancedJava Server Faces (JSF) - advanced
Java Server Faces (JSF) - advanced
BG Java EE Course
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
vikram singh
 
Screenshot of Desktop System Application Complete
Screenshot of Desktop System Application CompleteScreenshot of Desktop System Application Complete
Screenshot of Desktop System Application Complete
Hendrawan Hendrawan
 
Linux directory structure by jitu mistry
Linux directory structure by jitu mistryLinux directory structure by jitu mistry
Linux directory structure by jitu mistry
JITU MISTRY
 
Joget Workflow v5 Training Slides - Module 16 - Preparing Development Environ...
Joget Workflow v5 Training Slides - Module 16 - Preparing Development Environ...Joget Workflow v5 Training Slides - Module 16 - Preparing Development Environ...
Joget Workflow v5 Training Slides - Module 16 - Preparing Development Environ...
Joget Workflow
 
WebService-Java
WebService-JavaWebService-Java
WebService-Java
halwal
 
Linux Directory Structure
Linux Directory StructureLinux Directory Structure
Linux Directory Structure
Kevin OBrien
 
Java Server Faces (JSF) - Basics
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
BG Java EE Course
 
3 java - variable type
3  java - variable type3  java - variable type
3 java - variable type
vinay arora
 
Ad

Similar to Web Applications and Deployment (20)

Introduction to Java Servlets and JSP (1).ppt
Introduction to Java Servlets and JSP (1).pptIntroduction to Java Servlets and JSP (1).ppt
Introduction to Java Servlets and JSP (1).ppt
ansariparveen06
 
Lect06 tomcat1
Lect06 tomcat1Lect06 tomcat1
Lect06 tomcat1
Phuc Truong Ba
 
bjhbj
bjhbjbjhbj
bjhbj
MohammedNasser364522
 
Tomcat Configuration (1)
Tomcat Configuration (1)Tomcat Configuration (1)
Tomcat Configuration (1)
nazeer pasha
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
Nitin Pai
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache Tomcat
Auwal Amshi
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
Adil Jafri
 
Auxiliary : Tomcat
Auxiliary : TomcatAuxiliary : Tomcat
Auxiliary : Tomcat
webhostingguy
 
Tomcat configuration
Tomcat configurationTomcat configuration
Tomcat configuration
Dima Gomaa
 
Unit5 servlets
Unit5 servletsUnit5 servlets
Unit5 servlets
Praveen Yadav
 
Tomcat tutorail
Tomcat tutorailTomcat tutorail
Tomcat tutorail
Supratim Ray
 
Apache Tomcat 8 Application Server
Apache Tomcat 8 Application ServerApache Tomcat 8 Application Server
Apache Tomcat 8 Application Server
mohamedmoharam
 
Java Servlets & JSP
Java Servlets & JSPJava Servlets & JSP
Java Servlets & JSP
Manjunatha RK
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
Ankit Minocha
 
SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3
Ben Abdallah Helmi
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
vishal choudhary
 
Web Application Deployment
Web Application DeploymentWeb Application Deployment
Web Application Deployment
elliando dias
 
SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3
Ben Abdallah Helmi
 
Jira Rev002
Jira Rev002Jira Rev002
Jira Rev002
Rich Helton
 
Tumbleweed intro
Tumbleweed introTumbleweed intro
Tumbleweed intro
Rich Helton
 
Introduction to Java Servlets and JSP (1).ppt
Introduction to Java Servlets and JSP (1).pptIntroduction to Java Servlets and JSP (1).ppt
Introduction to Java Servlets and JSP (1).ppt
ansariparveen06
 
Tomcat Configuration (1)
Tomcat Configuration (1)Tomcat Configuration (1)
Tomcat Configuration (1)
nazeer pasha
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
Nitin Pai
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache Tomcat
Auwal Amshi
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
Adil Jafri
 
Tomcat configuration
Tomcat configurationTomcat configuration
Tomcat configuration
Dima Gomaa
 
Apache Tomcat 8 Application Server
Apache Tomcat 8 Application ServerApache Tomcat 8 Application Server
Apache Tomcat 8 Application Server
mohamedmoharam
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
Ankit Minocha
 
SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3
Ben Abdallah Helmi
 
Web Application Deployment
Web Application DeploymentWeb Application Deployment
Web Application Deployment
elliando dias
 
SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3
Ben Abdallah Helmi
 
Tumbleweed intro
Tumbleweed introTumbleweed intro
Tumbleweed intro
Rich Helton
 
Ad

More from BG Java EE Course (20)

JSP Custom Tags
JSP Custom TagsJSP Custom Tags
JSP Custom Tags
BG Java EE Course
 
JSTL
JSTLJSTL
JSTL
BG Java EE Course
 
Unified Expression Language
Unified Expression LanguageUnified Expression Language
Unified Expression Language
BG Java EE Course
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
BG Java EE Course
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
CSS
CSSCSS
CSS
BG Java EE Course
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
BG Java EE Course
 
HTML Fundamentals
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
BG Java EE Course
 
WWW and HTTP
WWW and HTTPWWW and HTTP
WWW and HTTP
BG Java EE Course
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course
 
Creating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSSCreating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSS
BG Java EE Course
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
BG Java EE Course
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
BG Java EE Course
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
BG Java EE Course
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
BG Java EE Course
 
Introduction to-RDBMS-systems
Introduction to-RDBMS-systemsIntroduction to-RDBMS-systems
Introduction to-RDBMS-systems
BG Java EE Course
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
BG Java EE Course
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
BG Java EE Course
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
BG Java EE Course
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
BG Java EE Course
 

Web Applications and Deployment

  • 1. Java Web Applications and Deployment Svetlin Nakov Borislava Spasova
  • 2. Contents Using Tomcat Web Application Server What is a Web Container? What is Apache Tomcat? Installing and Running Tomcat Tomcat Directory Structure 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
  • 3. Using Tomcat Web Application Server
  • 4. 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 do Post (…) 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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=&quot;C:\Program Files\Java\jdk1. 6 .0_ 23&quot;
  • 9. Tomcat Directory Structure (as in Tomcat 7.0.11) $CATALINA_HOME = <some_dir>/apache-tomcat- 7 . 0 . 1 1 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 for holding temp files and directories
  • 10. Java Web Applications Structure and Deployment
  • 11. 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 W eb AR chive File (.WAR file)
  • 12. 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 by the application (servlets, beans, etc.) web.xml Special configuration file called &quot; Web application deployment descriptor &quot;
  • 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. 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
  • 15. WAR Files – Example With the Tomcat distribution comes an example WAR file : sample.war CATALINA_HOME\webapps\docs\appdev\sample\sample.war
  • 16. 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 &quot;hot deploy&quot; option Basic format of web.xml file: Order of the elements within the <web-app> tag is important! <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. 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
  • 18. 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-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. Adding initialization parameters to servlets Accessing the servlet initialization parameters 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. 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. Creating Web Application and Deploying on Tomcat Create a new directory myapp in CATALINA_HOME\webapps Create subdirectories WEB-INF , WEB-INF/classes and WEB-INF/lib Compile the HelloServlet.java and copy the class file HelloServlet.class to WEB-INF/classes/ Copy the file HelloForm.html to the application root directory myapp / Create the file deployment descriptor file WEB-INF/web.xml
  • 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. Creating Web Application and Deploying on Tomcat (3) Using WinZip or normal jar-tool, wrap the complete Web application into a portable Web archive ( myapp.war ) Delete the directory myapp (leave only the file myapp.war in TOMCAT_HOME/webapps ) Start Tomcat and ensure that the application has been deployed (look at the console logs) Browse the deployed Web application: http://localhost:8080/myapp/HelloForm.html
  • 24. Problems Download and install Tomcat (use the ZIP distribution, not the Windows executable). Deploy and r un the sample application from: CATALINA_HOME\webapps\docs\appdev\sample\sample.war 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 .
  • 25. Homework Download and install Tomcat at home. Use the ZIP distribution, not the Windows executable. 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. Find information in Google about servlet filters . Add a servlet filter to the application that show current date and time on each page.

Editor's Notes

  • #13: webapp directory is considered the Web application root directory. All JSP, HTML, JavaScript files, and other resources are under this directory. The WEB-INF directory contains resources used by the application, WEB-INF is not in the public document root—no files contained in this directory structure are accessible by a client. The classes directory (under WEB-INF) contains servlets, beans, and utility classes needed for webapp&apos;s operation. lib directory (under WEB-INF) contains Java archive files (JARs), such as the JDBC driver or tag library, on which webapp depends. If a class is present in a JAR file and in the classes directory, the class loader loads the one in the classes directory.
  • #19: Notes Order matters: servlet before servlet-mapping JSP: Use jsp-file instead of servlet-class