Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... ·...

of 24 /24
1 Module 5 Developing with JavaServer Pages Technology Developing with JSP Technology 5 126 Omega Developing Applications for Java EE Platform Evaluate the role of JSP technology as a presentation Mechanism Author JSP pages Process data received from servlets in a JSP page Describe the use of tag libraries Objectives

Transcript of Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... ·...

Page 1: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

1

Module 5Developing withJavaServer PagesTechnology

Dev

elop

ing

with

JS

P T

echn

olog

y5

126 Ω Omega ΩDeveloping Applications for Java EE Platform

Evaluate the role of JSP technology as a presentationMechanism Author JSP pages Process data received from servlets in a JSP page Describe the use of tag libraries

Objectives

Page 2: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

2

Dev

elop

ing

with

JS

P T

echn

olog

y5

127 Ω Omega ΩDeveloping Applications for Java EE Platform

JSP pages are text-based documents that describe how to process a request and create a response. Using JSP technology, a page designer creates a document to generate dynamic content. JSP elements:

• Enable external object access• Add canned programming capabilities

Source files for JSP pages typically end with the .jspextension.

JSP Technology as a Presentation

Mechanism

Dev

elop

ing

with

JS

P T

echn

olog

y5

128 Ω Omega ΩDeveloping Applications for Java EE Platform

JSP technology: Uses beans to interact with server-side objects Uses tag libraries to develop and extend the cannedcapabilities provided by actions Allows for a high degree of separation between the static and dynamic content in a JSP page Provides (where necessary) a powerful scripting language for JSP pages Is an integral part of the Java EE platform, and so provides front-end access to EJB components

JSP Technology as a Presentation

Mechanism

Page 3: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

3

Dev

elop

ing

with

JS

P T

echn

olog

y5

129 Ω Omega ΩDeveloping Applications for Java EE Platform

JSP pages are web components that are based on the servlet model and that run as servlets:

Presentation Using JSP Pages Compared

to Servlets

Dev

elop

ing

with

JS

P T

echn

olog

y5

130 Ω Omega ΩDeveloping Applications for Java EE Platform

Presentation Using JSP Pages Compared

to Servlets

Page 4: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

4

Dev

elop

ing

with

JS

P T

echn

olog

y5

131 Ω Omega ΩDeveloping Applications for Java EE Platform

You can separate programmatic functionality from presentation in JSP components in two ways: Incorporate classes with the <jsp:useBean> tag and theJSTL:

• Useful for carrying data into the JSP component• JSTL allows programmatic behavior without scriptlets

Make use of custom tag libraries:• Most valuable when tag libraries are general-purpose

and reusable• Less useful for page specific logic, such as unique form

processing

Worker Beans, JSTL, and Custom Tags

Dev

elop

ing

with

JS

P T

echn

olog

y5

132 Ω Omega ΩDeveloping Applications for Java EE Platform

JSP pages: Are translated into servlets on demand Can be deployed in the same manner as an HTML page, by copying the file onto the server

JSP Page Deployment Mechanism

Page 5: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

5

Dev

elop

ing

with

JS

P T

echn

olog

y5

133 Ω Omega ΩDeveloping Applications for Java EE Platform

JSP Page Translation Procedure

Dev

elop

ing

with

JS

P T

echn

olog

y5

134 Ω Omega ΩDeveloping Applications for Java EE Platform

Ideally, a JSP page should be concerned with presentation logic only. A servlet is a better alternative for processing logic and for flow control. Using scripting code for processing logic and flow control within a JSP page results in problems that include: A JSP page author:

• Must code well in the scripting language• Might need more business domain knowledge

It is more difficult to see presentation information when you view the JSP page. Debugging is more difficult because of the added complexity and decreased clarity.

Java Code Embedded in JSP Pages

Page 6: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

6

Dev

elop

ing

with

JS

P T

echn

olog

y5

135 Ω Omega ΩDeveloping Applications for Java EE Platform

A JSP page contains: Standard markup tags, such as HTML or XML Associated text data A variety of elements that are defined by the JSPspecification

Authoring JSP Pages

Dev

elop

ing

with

JS

P T

echn

olog

y5

136 Ω Omega ΩDeveloping Applications for Java EE Platform

JSP Page Components

Page 7: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

7

Dev

elop

ing

with

JS

P T

echn

olog

y5

137 Ω Omega ΩDeveloping Applications for Java EE Platform

Syntactic forms of tags based on JSP tags can be represented in two different ways:

Syntactic Forms of JSP Tags

Dev

elop

ing

with

JS

P T

echn

olog

y5

138 Ω Omega ΩDeveloping Applications for Java EE Platform

Contain information to help a JSP container configure andrun a JSP page Are associated with the compiled servlet that is created from the JSP page Do not produce output Have the following generic syntax:

<%@ directive attribute="value" ... %>

JSP Technology Directives

Page 8: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

8

Dev

elop

ing

with

JS

P T

echn

olog

y5

139 Ω Omega ΩDeveloping Applications for Java EE Platform

JSP Directives

Dev

elop

ing

with

JS

P T

echn

olog

y5

140 Ω Omega ΩDeveloping Applications for Java EE Platform

The page directive defines page-dependent attributes: An attribute and value pair cannot be redefined within atranslation unit, with the exception of the include pagedirective. Redefining a page directive results in a fatal translation error, unless the new and the old definitions are the same.

The page Directive

Page 9: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

9

Dev

elop

ing

with

JS

P T

echn

olog

y5

141 Ω Omega ΩDeveloping Applications for Java EE Platform

Examples using both styles of syntax:

<%@ page import="java.util.*, java.lang.*" %>

<%@ page buffer="5kb" autoFlush="false" %>

<jsp:directive.page errorPage="error.jsp" />

The page Directive

Dev

elop

ing

with

JS

P T

echn

olog

y5

142 Ω Omega ΩDeveloping Applications for Java EE Platform

JSP page Directives

Page 10: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

10

Dev

elop

ing

with

JS

P T

echn

olog

y5

143 Ω Omega ΩDeveloping Applications for Java EE Platform

The include Directive

The include directive: Inserts the text of the specified resource into the .jsp file at page translation time Treats resources as static objects Can be other HTML files or other JSP pages that contain text, or code, or both

Examples of the include directive:<%@ include file="relativeURL" %>

Or<jsp:directive.include file="relativeURL" />

Dev

elop

ing

with

JS

P T

echn

olog

y5

144 Ω Omega ΩDeveloping Applications for Java EE Platform

The include Directive

Page 11: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

11

Dev

elop

ing

with

JS

P T

echn

olog

y5

145 Ω Omega ΩDeveloping Applications for Java EE Platform

Declarations, Expressions, and Scriptlets

Scripting elements allow a page designer to provide advanced programming capabilities. Scripting elements include: Declarations Expressions Scriptlets

Dev

elop

ing

with

JS

P T

echn

olog

y5

146 Ω Omega ΩDeveloping Applications for Java EE Platform

Declarations, Expressions, and Scriptlets

Page 12: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

12

Dev

elop

ing

with

JS

P T

echn

olog

y5

147 Ω Omega ΩDeveloping Applications for Java EE Platform

Declaration Scripting Elements

Dev

elop

ing

with

JS

P T

echn

olog

y5

148 Ω Omega ΩDeveloping Applications for Java EE Platform

Expression Scripting Elements

Page 13: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

13

Dev

elop

ing

with

JS

P T

echn

olog

y5

149 Ω Omega ΩDeveloping Applications for Java EE Platform

Scriptlet Scripting Elements

Dev

elop

ing

with

JS

P T

echn

olog

y5

150 Ω Omega ΩDeveloping Applications for Java EE Platform

Scriptlet Scripting Elements

Page 14: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

14

Dev

elop

ing

with

JS

P T

echn

olog

y5

151 Ω Omega ΩDeveloping Applications for Java EE Platform

Scriptlet Scripting Elements

Translate to

Dev

elop

ing

with

JS

P T

echn

olog

y5

152 Ω Omega ΩDeveloping Applications for Java EE Platform

Thread-Safety Implications

Declarations occur at the instance level of the generated servlet. Therefore: All requests to the JSP page share these variables and methods. Thread-safety problems can occur with this technique.

All of the cautions that apply to servlets and thread-safety, apply to JSP page declarations.

Page 15: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

15

Dev

elop

ing

with

JS

P T

echn

olog

y5

153 Ω Omega ΩDeveloping Applications for Java EE Platform

Processing Data From Servlets

The JSP specification defines a standard set of action types that all JSP containers must implement, including: Create or use beans Set and get bean properties Include static and dynamic resources in the current page’s contextYou can define additional action types using custom tag libraries.

Dev

elop

ing

with

JS

P T

echn

olog

y5

154 Ω Omega ΩDeveloping Applications for Java EE Platform

The jsp:useBean Action

Creates or locates an existing bean that matches the criteria in the tag Associates the bean instance with a scope and action ID Makes the ID accessible by scripting elements and custom tags

Page 16: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

16

Dev

elop

ing

with

JS

P T

echn

olog

y5

155 Ω Omega ΩDeveloping Applications for Java EE Platform

The jsp:useBean Action

Syntax of jsp:usebean:<jsp:useBean id="name" scope="scope" typeSpec />

Alternate syntax with initialization code:<jsp:useBean id="name" scope="scope" typeSpec>

<% ...initialization code... %>

</jsp:useBean>

typeSpec can be any of the following:class="className"

class="className" type="typeName"

beanName="beanName" type=" typeName"

type="typeName"

Dev

elop

ing

with

JS

P T

echn

olog

y5

156 Ω Omega ΩDeveloping Applications for Java EE Platform

The jsp:useBean Action

Page 17: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

17

Dev

elop

ing

with

JS

P T

echn

olog

y5

157 Ω Omega ΩDeveloping Applications for Java EE Platform

The jsp:useBean Action

This graphic shows the id attribute

Dev

elop

ing

with

JS

P T

echn

olog

y5

158 Ω Omega ΩDeveloping Applications for Java EE Platform

The jsp:useBean Action

Examples of jsp:useBean:• Usage example for the id attribute:<jsp:useBean id="account” class="bank.Account”/>

• To retrieve the account balance, use the expression:<%=account.getBalance() %>

Page 18: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

18

Dev

elop

ing

with

JS

P T

echn

olog

y5

159 Ω Omega ΩDeveloping Applications for Java EE Platform

The jsp:useBean Scopes

Dev

elop

ing

with

JS

P T

echn

olog

y5

160 Ω Omega ΩDeveloping Applications for Java EE Platform

Request-Scope Beans and Collecting Data

From Servlets

The jsp:useBean action is commonly used to share data between servlets and JSP pages. The following is a typical sequence of events involving jsp:useBean: A servlet performs front-end processing. The servlet sets an attribute on the request object. The servlet dispatches to a JSP page to display dynamic data. The JSP uses jsp:useBean with the request scope attribute to collect the data.

Page 19: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

19

Dev

elop

ing

with

JS

P T

echn

olog

y5

161 Ω Omega ΩDeveloping Applications for Java EE Platform

Request-Scope Beans and Collecting Data

From Servlets

The following code snippet creates a new Customer object and saves it in a request attribute named customer:

Dev

elop

ing

with

JS

P T

echn

olog

y5

162 Ω Omega ΩDeveloping Applications for Java EE Platform

Request-Scope Beans and Collecting Data

From ServletsThe JSP page example.jsp that is illustrated in the following code snippet can then access and process the customer object as follows:

Page 20: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

20

Dev

elop

ing

with

JS

P T

echn

olog

y5

163 Ω Omega ΩDeveloping Applications for Java EE Platform

The jsp:getProperty Action

Dev

elop

ing

with

JS

P T

echn

olog

y5

164 Ω Omega ΩDeveloping Applications for Java EE Platform

Custom Tag Libraries

Provide an alternative to scriptlets for customizing JSP pages for specific business needs

It is preferable to avoid scriptlet code in JSP pages. Use an XML-style tag format to provide canned capabilities to use, modify, or create objects

Page 21: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

21

Dev

elop

ing

with

JS

P T

echn

olog

y5

165 Ω Omega ΩDeveloping Applications for Java EE Platform

The taglib Directive

Extends the set of tags that a JSP container can interpret Associates a tag prefix with a tag library that:

• Consists of a set of classes and a tag library descriptor• Implements the range of operations defined in the tags

Dev

elop

ing

with

JS

P T

echn

olog

y5

166 Ω Omega ΩDeveloping Applications for Java EE Platform

The taglib Directive

Format of the taglib directive:<%@ taglib uri="iterator_tags” prefix="iterator"%>

Tag library usage:<iterator:iterate>

<%-- perform repetitive task --%>

...

</iterator:iterate>

Page 22: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

22

Dev

elop

ing

with

JS

P T

echn

olog

y5

167 Ω Omega ΩDeveloping Applications for Java EE Platform

The tag-library Descriptor and Java Classes

The URI mapping in the web application deployment descriptor maps a prefix to a TLD. The TLD is an XML file that is usually packaged in the web application or library, along with the classes that implement its range of operations.

Dev

elop

ing

with

JS

P T

echn

olog

y5

168 Ω Omega ΩDeveloping Applications for Java EE Platform

The tag-library Descriptor and Java Classes

Mapping example: In the JSP page:

<iterator:iterate id=”accounts”>

Information from the TLD:

Page 23: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

23

Dev

elop

ing

with

JS

P T

echn

olog

y5

169 Ω Omega ΩDeveloping Applications for Java EE Platform

The Expression Language (EL)

The Expression Language is an easy to use language that can be embedded in JSP pages instead of scriptlets (when used with tag libraries). Its syntax is similar to JavaScript and can be learned by non-programmers.

$ 3 + 2

$ param.address

$ requestScope.customer.name

$ not empty sessionScope.message

Dev

elop

ing

with

JS

P T

echn

olog

y5

170 Ω Omega ΩDeveloping Applications for Java EE Platform

JSTL Examples

Page 24: Module 5 Developing with JavaServer Pages Technologybkurt/Courses/bte531/bte531.module.05... · Developing Applications for Java EE Platform 127 Ω Omega Ω JSP pages are text-based

24

Dev

elop

ing

with

JS

P T

echn

olog

y5

171 Ω Omega ΩDeveloping Applications for Java EE Platform

Packaging Tag Libraries in Web Applications

With a custom tag library, the tag class must be in the classpath of the class loader. Tag classes can be located in the same directories as regular classes and jar libraries. A Java EE 5 application server implementation can support additional directories when determining the classpath. The standard tag libraries are already present on any Java EE 5 system as JAR files. JSTL JAR files have TLD files embedded in such a way that they do not require them to be listed in the web.xml file.