Web Applications Basics - PowerPoint PPT Presentation

About This Presentation
Title:

Web Applications Basics

Description:

Web Applications Basics Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that is responsible ... – PowerPoint PPT presentation

Number of Views:168
Avg rating:3.0/5.0
Slides: 35
Provided by: ltnLvaps
Category:

less

Transcript and Presenter's Notes

Title: Web Applications Basics


1
Web Applications Basics
2
Introduction to Web
  • Web features
  • Clent/Server
  • HTTP
  • HyperText Markup Language
  • URL addresses
  • Web server - a computer program that is
    responsible for accepting HTTP requests from
    clients and serving them HTTP responses
  • Web application - a dynamic extension of a web or
    application server

3
Web Applications Components
  • Two types of web applications
  • Presentation-oriented (HTML, XML pages)
  • Service-oriented (Web services)
  • Web components provide the dynamic extension
    capabilities for a web server
  • Java servlets
  • JSP pages
  • Web service endpoints

4
Web Application Interaction
  • client sends an HTTP request to the web server
  • web server HTTP request ? HTTPServletRequest
  • This object is delivered to a web component,
    which can interact with JavaBeans or a DB to
    generate dynamic content
  • web component generates an HTTPServletResponse
    or pass the request to another web component
  • web server HTTPServletResponse ? HTTP response
  • web server returns HTTP response to the client

5
Web Application Interaction
6
Web Components
  • Servlets - Java classes that dynamically process
    requests and construct responses
  • JSP pages - text-based documents that execute as
    servlets but allow a more natural approach to
    creating static content
  • Appropriate usage
  • Servlets - service-oriented applications, control
    functions
  • JSP - generating text-based markup (HTML,
    SVG, WML, XML)

7
Java Web Application Technologies
  • Java Servlet technology is the foundation of all
    the web application technologies

8
Web Containers
  • Web components are supported by the services of a
    runtime platform called a web container
  • In J2EE, a web container "implements the web
    component contract of the J2EE architecture
  • Web container services
  • request dispatching
  • security
  • concurrency
  • life-cycle management
  • naming, transactions, email APIs

9
Web Container Examples
  • Non-commercial
  • Apache Tomcat
  • Jetty
  • Commertial
  • Sun Java System Application Server
  • BEA WebLogic Server
  • Oracle Application Server
  • WebSphere
  • Open source
  • JBoss

10
Deployment
  • Web components have to be installed or deployed
    to the web container
  • Aspects of web application behaviour can be
    configured during application deployment
  • The configuration information is maintained in a
    XML file called a web application deployment
    descriptor

11
Web Application Development
  • A web application consists of
  • Web components
  • Static resource files (such as images)
  • Helper classes and libraries
  • The process for creating and running a web
    application is different from that of traditional
    stand-alone Java classes

12
Development Cycle
  1. Develop the web component code
  2. Develop the web application deployment descriptor
  3. Compile the web application components and helper
    classes referenced by the components
  4. Optionally package the application into a
    deployable unit
  5. Deploy the application into a web container
  6. Access a URL that references the web application

13
Web Modules
  • According to Java EE architecture and Java
    Servlet Specification
  • Web components and static web content files such
    as images are called web resources
  • A web module is the smallest deployable and
    usable unit of web resources
  • Web module corresponds to a web application
  • A web module has a specific structure

14
Web Module Structure
  • The top-level directory
  • of a web module is the
  • document root of
  • the application
  • The document root contains
  • JSP pages
  • client-side classes
  • client-side archives
  • static web resources

15
Web Module Structure
  • The document root contains
  • a subdirectory /WEB-INF/
  • web.xml web application
  • deployment descriptor
  • lib JAR archives of
  • libraries called by
  • server-side classes

16
Web Module Structure
  • classes server-side classes
  • servlets
  • utility classes
  • JavaBeans components
  • tags tag files, which are
  • implementations of
  • tag libraries

17
Configuring Web Applications
  • Web applications are configured via
    /WEB-INF/web.xml file
  • Configuration options
  • Map URLs to web components
  • Set initialization parameters
  • Map errors to error screens
  • Declare welcome files
  • Declare resource references

18
Mapping URLs to Web Components
  • When a request is received by the web container
    it must determine which web component should
    handle the request
  • Need to add a servlet definition and a servlet
    mapping for each servlet to web.xml file

ltservletgt ltservlet-namegtServletNamelt/servlet-na
megt ltservlet-classgtServletClasslt/servlet-classgt
lt/servletgt ltservlet-mappinggt
ltservlet-namegtServletNamelt/servlet-namegt
lturl-patterngt/pathlt/url-patterngt lt/servlet-mapping
gt
19
Initialization Parameters
  • It's possible to pass initialization parameters
    to the context or to a web component
  • Context parameters
  • ltcontext-paramgt
  • ltparam-namegtnamelt/param-namegt
  • ltparam-valuegtvaluelt/param-valuegt
  • lt/context-paramgt
  • Servlet parameters (within servlet definition)
  • ltinit-paramgt
  • ltparam-namegtnamelt/param-namegt
  • ltparam-valuegtvaluelt/param-valuegt
  • lt/init-paramgt

20
Handling Errors
  • Web container generates default error page
  • You can specify custom default page to be
    displayed instead
  • Steps to handle errors
  • Create appropriate error html pages for error
    conditions
  • Modify the web.xml accordingly

21
Example Setting Error Pages
  • lterror-pagegt
  • ltexception-typegt
  • exception.BookNotFoundException
  • lt/exception-typegt
  • ltlocationgt/errorpage1.htmllt/locationgt
  • lt/error-pagegt
  • lterror-pagegt
  • ltexception-typegt
  • exception.BooksNotFoundException
  • lt/exception-typegt
  • ltlocationgt/errorpage2.htmllt/locationgt
  • lt/error-pagegt
  • lterror-pagegt
  • ltexception-typegt
  • exception.OrderException
  • lt/exception-typegt
  • ltlocationgt/errorpage3.htmllt/locationgt
  • lt/error-pagegt

22
Example web.xml
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • lt!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
    Inc.//DTD Web Application 2.3//EN
    "http//java.sun.com/dtd/web-app_2_3.dtd"gt
  • ltweb-appgt
  • ltdisplay-namegtYour team project
    namelt/display-namegt
  • ltdescriptiongtTeam N servletslt/descriptiongt
  • ltcontext-paramgt
  • ltparam-namegtname_of_context_initialization_p
    arameterlt/param-namegt
  • ltparam-valuegtvalue_of_context_initializtion
    _parameterlt/param-valuegt
  • lt/context-paramgt
  • ltservletgt
  • ltservlet-namegtYourServletlt/servlet-namegt
  • ltservlet-classgtYourServletClasslt/servlet-classgt
  • lt/servletgt
  • ltservlet-mappinggt
  • ltservlet-namegtYourServletlt/servlet-namegt
  • lturl-patterngt/YourServletlt/url-patterngt
  • lt/servlet-mappinggt
  • lt/web-appgt

23
WAR Files
  • A web module can be deployed as an unpacked file
    structure or can be packaged in a JAR file known
    as a Web Archive File
  • WAR file can be created by
  • executing jar command
  • using Ant target
  • using IDE (Eclipse for instance)
  • using Maven

24
Setting a Context Root
  • A context root identifies a web application in a
    Java EE server
  • The server is responsible for mapping URLs that
    start with a specific prefix to the location of a
    web application
  • Usually this is done with a web server
    configuration file

25
Using Maven Jetty
  • A convenient way to develop, build, deploy and
    run Web application is by using
  • Maven build tool
  • http//maven.apache.org/
  • Jetty web server
  • http//www.mortbay.org/

26
Creating Directory Structure
  • Maven 2 supports the notion of creating a
    complete project template with a simple command
  • To create Web project template need to use
    maven-archetype-webapp archetype

mvn archetypecreate -DgroupIdcom.maven2exampl
e -DartifactIdmaven2example_webapp
-DarchetypeArtifactIdmaven-archetype-webapp
27
Maven Web Directory Structure
  • ltrootgt/src/main/webapp/
  • - directory structure for a WAR

28
Packaging
  • Executing the command
  • mvn package
  • creates a WAR file

29
Running with Jetty
  • Its easy to run application by using Jetty
    plugin for Maven
  • Jetty is an open-source, standards-based,
    full-featured web server implemented entirely in
    Java
  • First created in 1995
  • Latest version 6.1.5 / July 23, 2007

30
Running with Jetty
  • Add the Jetty plugin to the pom.xml

ltbuildgt ltfinalNamegtmaven2example_webapplt/fina
lNamegt ltpluginsgt ltplugingt
ltgroupIdgtorg.mortbay.jettylt/groupIdgt
ltartifactIdgtmaven-jetty-pluginlt/artifactIdgt
ltversiongt6.0.1lt/versiongt lt/plugingt
lt/pluginsgt lt/buildgt
31
Running with Jetty
  • Execute mvn jettyrun command

gtmvn jettyrun INFO Scanning for
projects... INFO Searching repository for
plugin with prefix 'jetty'. INFO
--------------------------------------------------
--- --- INFO Building maven2example_webapp
Maven Webapp INFO task-segment
jettyrun INFO -------------------------------
---------------------- ... INFO Starting jetty
6.0.1 ... ... INFO Started Jetty Server
  • Stop by CtrlC

32
Opening the Application
  • Open your web browser to
  • http//localhost8080/

33
Opening the Application
  • Valid URL is
  • http//localhost8080/maven2example_webapp/

34
Resources
  • J2EE Tutorial Getting Started with Web
    applications
  • http//java.sun.com/javaee/5/docs/tutorial/doc/bn
    adr.html
  • Building Web Applications with Maven 2
  • http//today.java.net/pub/a/today/2007/03/01/buil
    ding-web-applications-with-maven-2.html
  • Filmina par Web 2 (5 minutes)
  • http//www.youtube.com/watch?v6gmP4nk0EOE
Write a Comment
User Comments (0)
About PowerShow.com