+ All Categories
Home > Documents > Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP &...

Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP &...

Date post: 11-Jan-2016
Category:
Upload: willa-eaton
View: 215 times
Download: 2 times
Share this document with a friend
39
Web Tier Technologies
Transcript
Page 1: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

Web Tier Technologies

Page 2: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Introduction

Web Tier JEE Patterns

Web Container

Servlets

JSP & JSTL

Expression Language

Java Server Faces

Agenda

Page 3: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Intercepting Filter

Front Controller

Context Object

Application Controller

View Helper

Composite View

Service to Worker

Dispatcher View

Web Tier JEE patterns

Page 4: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Request based MVC framework – Aware of HttpServletRequest

Struts, Spring MVC

Component based MVC framework

JSF, Wicket, Tapestry

Web Tier Architectural Pattern

Page 5: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Model View Controller- separation of concerns

Page 6: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Model View Controller

Features of MVC 1

Html or jsp files are used to code the presentation. To retrieve the data JavaBean can be used. In MVC1

architecture all the view, control elements are implemented using Servlets or Jsp.

In MVC1 there is tight coupling between page and model as data access is usually done using Custom tag or

through java bean call.

Features of MVC 2

The MVC2 architecture removes the page centric property of MVC1 architecture by separating Presentation, control

logic and the application state.

In MVC2 architecture there is only one controller which receives all the request for the application and is

responsible for taking appropriate action in response to each request.

Page 7: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

View Type

Controller based View

HTML tag mixed with Java code within Servlet

Template based View

JSP

Freemarker

Velocity

WebMacro

Page 8: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Web application framework

Apache Struts

Spring MVC

Apache Tapestry

Apache Wicket

Java Server Faces

Grails

Google web toolki

JBoss Seam

Zk

AppFuse

FormEngine

Hamlet

OpenXava

Ztemplates

WaveMaker

Toolkit

Page 9: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Containers

Java EE architecture has different containers:

• A Web Container for hosting Java Servlets and JSP pages.

• A EJB container for hosting Enterprise JavaBean components.

• An applet container to run applets,

• An application client container for running standard Java application clients.

What is a Container ?

Containers are the interface between a component and the low-level platform specific functionality that supports the component.

How does it work

Container settings are specified for each component during assembly process before deployment.

What do I get

Support for security, transaction management, JNDI lookup, Remote connectivity.

Page 10: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Web Container Example

Apache Tomcat

Resin

Jetty

Apache Geronimo Application Server

Jboss Application Server

Weblogic Application Server

Websphere Application Server

Tc server

NetWeaver

Page 11: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Web Container

The Web container is the part of the application server in which Web application components run.

Web application components include servlets, JSP files, and HTML files.

The Web container processes the web components. Each application server runtime has one logical Web

container, which can be modified, but not created or removed. Each Web container provides the following.

• Communications support

• Lifecycle management

• Multi-threading support

• Declarative security

• JSP Support

Page 12: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Web Container Contd..

Every web app contains a deployment descriptor called web.xml

It describes the web application to the servlet container.

It is an XML file

Web Application Property Short Description

Servlet Declarations Used to specify servlet properties

Servlet Mappings Used to specify URL to servlet mapping.

Application Lifecycle Listener classes Used to specify listener classes for HttpSession-Events and ServletContextAttributeEvent

ServletContext Init Parameters Used to specify initialization parameters for the web application

Error Pages Used to specify error pages for error conditions

Session Configuration Used to specify session timeout

Security Constraints Used to specify security requirements of the web application

Tag libraries Used to specify the tag libraries required by JSP Pages

Welcome File list Used to specify the welcome files for the web Application

Filter Definitions and Filter Mappings Used to specify the filter

MIME Type Mappings Used to specify MIME types for common file extensions.

JNDI names Used to specify JNDI names of the EJBs

Page 13: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Sample Web.xml

Page 14: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

A Servlet is a java technology-based Web component, managed by a container, that generates dynamic content

– is a simple, consistent mechanism for extending the functionality of a web server

– Are pre-compiled Java programs that are executed on the server side.– Require a Servlet container to run in

What is Servlet?

Page 15: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Servlet Interface

Methods to manage servlet

Generic Servlet

Implements Servlet

HttpServlet

Extends GenericServlet

Exposes HTTP – specific functionality

Servlet ServletConfig Serialization

GenericServlet

HttpServlet

UserDefinedServlet

Interface

Built-in Classes

User defined

Servlet Architecture Overview

Page 16: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Load

Initialize

Service

Destroy

Servlet Life Cycle

Page 17: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Every web application has exactly one instance of javax.servlet.Servlet-Context (assuming that the servlet container is not distributed across multiple JVMs).

The context is initialized at the time that the web application is loaded

Initialization parameters for a servlet context can be defined in Deployment descriptor

The servlets of a web application can retrieve these initialization parameters using the methods of the ServletContext interface as shown below

The servlet context initialization parameters are used to specify application-wide information, such as the developer’s contact information and the database connection information.

public void init()

{

ServletContext context = getServletConfig().getServletContext();

//ServletContext context = getServletContext();

String dburl = context.getInitParameter("dburl");

//use the dburl to create database connections

}

Uses ServletConfig to get ServletContext

Servlet Context

Page 18: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Filters are server-side components hosted by the web container that receive an inbound request before it is received by any other component

Filters are deployed in the deployment descriptor file web.xml and then map to either servlet names or URL patterns in application's deployment descriptor.

<filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param></filter><filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>

Filters

Page 19: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Listeners are server-side components hosted by the web container that are notified about specific events that occur during a Servlet’s lifecycle.

Listeners are deployed in the deployment descriptor file web.xml and then map to either servlet names or URL patterns in application's deployment descriptor.

Process of defining Listener Interfaces

Write a class that implements the corresponding listener interface

Specify the class name in the deployment descriptor

Servlet container will call the appropriate methods on objects of this class when the events occur

<listener><listener-class>

com.listeners.MyContextListener</listener-class>

</listener>

Listeners

Page 20: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Work well in a Heterogeneous Environments

– OS and platform neutral

– Work with all major web servers (IIS, Apache,etc..)

Clean separation of controller / logic from presentation

Efficient, scales very well

Well defined Web Architecture framework

– Standard built in services such as: Standard Approach to Authentication using declarative security vice programmatic

security Database connection pooling Complete support for sessions via cookies and/or URL re-writing

– Has automatic fallback to URL re-writing

Why Use Servlets

Page 21: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Java Server Pages (JSP)– A simplified, fast way to create dynamic web content– HTML or XML pages with embedded Java Code or Java Beans– Can be a mix of template data in HTML/XML with some dynamic content– A JSP is a complied to a Java Servlet automatically by the Servlet container, it is then

cached

What is Java Server Page

Page 22: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Used in JSP pages, pages that end *.jsp

Comment <%-- Comment --%>

Declaration <%! int x = 0; %>

Expression <%= expression %>

– Outputs to the Response stream

– Like a “printf” to the browser

– Do NOT use semi-colon to terminate the line

Scriplets - contains Java Code

– <% code fragments %>

Implicit objects always available in the JSP Page

– request – Http Request Object to get HTTP Headers, paramters passed from servlet

– response – Http Response Object

– session – Http Session Object

– pageContext

– application

JSP Construct

Page 23: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JSP Directives

– Are messages or instructions to the JSP container

– Do not produce any output

– “page” directive

• <%@ page import=“com.lucek.*” %>

• Commonly used for importing class paths

– “include” directive

• <%@ include file=“header.htm” %>

• Good for including static content

– “taglib” – lists the tag library descriptor location

• Required when using tab libraries

JSP Construct Contd.

Page 24: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

The ultimate goal of JSTL is to help simplify JavaServer Pages.JSTL offers the following capabilities

– General-purpose actions

• These actions complement the expression language by allowing a page author to easily display expressions in the expression language, set and remove the value of JSP scoped attributes, as well as catch exceptions

– Control flow actions

• Tag-based control flow structures (conditionals, iterators), which are more natural to page authors.

– Tag library validators

• Allow projects to only allow specific tag libraries, as well as enforce JSPcoding styles that are free of scripting elements.

All the capabilities are fulfilled using the tab libraries

JSTL

Core (javax.servlet.jsp.jstl.core) General purpose actions

Format (javax.servlet.jsp.jstl.fmt) Internationalization and Localization

SQL (javax.servlet.jsp.jstl.sql) Common database tasks

Tag Library Validator(javax.servlet.jsp.jstl.tlv)

Tag Library Validators

Page 25: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans

components. JSP EL allows you to create expressions both (a) arithmetic and (b) logical.

The EL is a simple language designed to meet the needs of the presentation layer in

web applications. It features:

– A simple syntax restricted to the evaluation of expressions

– Variables and nested properties

– Relational, logical, arithmetic, conditional, and empty operators

– Functions implemented as static methods on Java classes

– Lenient semantics where appropriate default values and type conversions are provided to minimize exposing errors to end users

JSP - Expression Language

<%Person p = (Person) request.getAttribute(“person”)

%>……….Person Name: <%= p.getName() %>………<% if (p.getAddress( ).equals(“defence”) ) { %> …….<% } %>

Person Name: $ { p.name }

<c:if test = “$ {p.address == param.add }” >

${ p.name }

</c:if>

Page 26: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

EL expressions can be used:

In static text, the value is computed and inserted in output.

In any standard or custom tag attribute that can accept an expression

example : <some:tag value="${expr}"/>

Expressions could be used

Variables

Implicit Objects

Literals

Operators

Using Expressions

Page 27: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JSF - Overview

• Holistic solution to several longstanding problems

– Tedious & repetitive coding

– Directly working with HTTP request/response

– Non availability of IDE

• Easier, more intuitive usage experience

• Developed by a community of Web application experts

• Combines

– Ubiquity of JSP and servlets

– Simplicity of JavaBeans

– Power of Java EE

– Common-sense of frameworks like Struts

Page 28: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Benefits of JSF

• Ease/Simplify Development

• Easy creation of UI

• Handles complexities of UI management

• Clean separation between presentation and logic

• Shorter development cycle

• Standard Java framework

• An extensible architecture

• Support for multiple client devices

• Flexible rendering model

• International language support

Page 29: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JSF and MVC

• JSF and JSP

– JSF UI comprised of JSP pages but not bound to that

– Lots of GUI tools to develop UI

• MVC model1 and model2

– Mixed java code with HTML: was easy to put business logic there

– Model1 : all backend code moved to java beans and use of JSTL EL

– Model2 : watered down version of MVC for web apps. Struts, Spring, Webwork etc.

• Richer MVC environment

– JSF component model

– Closer to true MVC programming than other model2 frameworks

– JSF gives host of event options but model2s rely on “request received”

• JSF's MVC implementation

– Faces Servlet is the controller

– Backing beans are the model

– JSP pages with custom JSF tags are the view

Page 30: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Struts vs JSFAdvantages of Struts

• Mature and proven

• Lots of documentation and reference materials

• Large developer base

• Broad tool and IDE support

• Open source

Advantages of JSF

• Standard UI component model

• Robust event handling mechanism

• Render kit support for different clients

• Highly 'pluggable' - components, view handler, etc

• Governed by JCP

When to go for JSF

– Most new applications

– Applications with sophisticated user interfaces

– Applications requiring multiple client support

– Smaller Struts applications that require significant upgrades

– Larger Struts applications that require significant upgrades

– Integrating selected JSF components into an existing Struts applications

Page 31: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Relationship to existing APIs

Page 32: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JSF Roles

Page 33: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

• FacesServlet Mapping

<!-- Faces Servlet -->

<servlet>

<servlet-name>Faces Servlet</servlet-name>

<servlet-class>

javax.faces.webapp.FacesServlet

</servlet-class>

<load-on-startup> 1 </load-on-startup>

</servlet>

<!-- Faces Servlet Mapping -->

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.jsf</url-pattern>

</servlet-mapping>

Configuring Web Application for JSF

Page 34: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JSF Application Configuration Files

• All JAR resources located in the /WEB-INF/lib

• Context initialization parameter javax.faces.application.CONFIG_FILES

in the web.xml

<context-param>

<param-name>

javax.faces.application.CONFIG_FILES

</param-name>

<param-value>

/WEB-INF/faces-config1.xml,

/WEB-INF/faces-config2.xml, ...

</param-value>

</context-param>

• faces-config.xml in the /WEB-INF/

Page 35: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Request Processing Life Cycle

<f:view> <h:form>

<p>Enter your username: <h:inputText value="#{LoginBean.username}"id="usernameTextField" required="true"/> <h:message for="usernameTextField" />

</p> <p>Enter your password:

<h:inputSecret value="#{LoginBean.password}"id="passwordTextField"required="true"/> <h:message for="passwordTextField" />

</p> <h:commandButton value="Submit Values"action="loginWelcome"/>

</h:form> </f:view>

<p>Enter your phone-number <h:inputText value="#{UserBean.phoneNumber}"id="phoneNumberTextField"required="true"> <f:validator validatorId="PhoneNumberValidator" /> </h:inputText> <h:message for="phoneNumberTextField"/>

</p>

Page 36: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JSF Managed/Backing Beans• Collect form input from components

• Properties can be synchronized with component values

• Can reference and manipulate UI component instances

• Handle UI events

• A combination of Struts ActionForms and Struts Actions

• Conceptually similar to code-behind classes in ASP.NET WebForms

• Usually talk to model objects to execute actual business logic

• Bind component’s value to backing bean property

• Usually configured using Managed Bean Creation facility

• Facility can be used for model objects as well

• Configured in JSF configuration file

• Object will be created automatically if it doesn't exist

Page 37: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JSF navigation Framework

• Provides navigation rules that allow you to define navigation from view to view (mostly JSP pages) in a Web application.

• Defined in JSF configuration files along with other definitions for a JSF application.

• Usually, this file is named faces-config.xml.

• However, you can assign any other name and even use more than one file to store JSF configuration data.

• Full support for declarative navigation

• Outcome of action methods used to select next page

• Eliminates need for Java code or JSPs to know file names

• To specify configuration files, use lines like the following in your web.xml file:

• Code:

<context-param>  <param-name>javax.faces.CONFIG_FILES</param-name>   <param-value>/WEB-INF/faces-config-navigation.xml,

/WEB-INF/faces-beans.xml

</param-value>

</context-param>

Page 38: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

Sample Navigation

<navigation-rule>     <from-view-id>/pages/inputname.jsp</from-view-id>

    <navigation-case>       <from-outcome>sayHello</from-outcome>       <to-view-id>/pages/greeting.jsp</to-view-id>     </navigation-case>

    <navigation-case>       <from-outcome>sayGoodbye</from-outcome>       <to-view-id>/pages/goodbye.jsp</to-view-id>     </navigation-case>

    <navigation-case>   <to-view-id>/pages/goodbye.jsp</to-view-id> </navigation-case>

</navigation-rule>

Page 39: Web Tier Technologies. July 14, 2010 Introduction Web Tier JEE Patterns Web Container Servlets JSP & JSTL Expression Language Java Server Faces Agenda.

July 14, 2010

JavaServer Pages 2.1 specification JSR 245 http://jcp.org/aboutJava/communityprocess/final/jsr245/

Servlet s 2.5 pecification JSR 154 http://jcp.org/aboutJava/communityprocess/mrel/jsr154/index.html

JSF 1.2 specification JSR 252 http://jcp.org/aboutJava/communityprocess/final/jsr252/index.html

JSTL 1.2 specification JSR 52 http://jcp.org/aboutJava/communityprocess/mrel/jsr052/index2.html

Java EE 5 specification JSR 244 http://jcp.org/aboutJava/communityprocess/final/jsr244/index.html

Web Tier Specifications


Recommended