Click here to load reader

Module 5 Developing with JavaServer Pages bkurt/Courses/bte531/bte531.module.05.developing... · PDF fileDeveloping with JavaServer Pages Technology Developing with JSP Technology

  • View
    214

  • Download
    0

Embed Size (px)

Text of Module 5 Developing with JavaServer Pages...

  • 1

    Module 5Developing withJavaServer PagesTechnology

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

  • 2

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

    wit

    h JS

    P Te

    chno

    logy

    5

    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

  • 3

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

    wit

    h JS

    P Te

    chno

    logy

    5

    130 Omega Developing Applications for Java EE Platform

    Presentation Using JSP Pages Compared

    to Servlets

  • 4

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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 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

    wit

    h JS

    P Te

    chno

    logy

    5

    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

  • 5

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    133 Omega Developing Applications for Java EE Platform

    JSP Page Translation Procedure

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

  • 6

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

    wit

    h JS

    P Te

    chno

    logy

    5

    136 Omega Developing Applications for Java EE Platform

    JSP Page Components

  • 7

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

    wit

    h JS

    P Te

    chno

    logy

    5

    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:

    JSP Technology Directives

  • 8

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    139 Omega Developing Applications for Java EE Platform

    JSP Directives

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

  • 9

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    141 Omega Developing Applications for Java EE Platform

    Examples using both styles of syntax:

    The page DirectiveD

    evel

    opin

    g w

    ith

    JSP

    Tech

    nolo

    gy5

    142 Omega Developing Applications for Java EE Platform

    JSP page Directives

  • 10

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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:

    Or

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    144 Omega Developing Applications for Java EE Platform

    The include Directive

  • 11

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

    wit

    h JS

    P Te

    chno

    logy

    5

    146 Omega Developing Applications for Java EE Platform

    Declarations, Expressions, and Scriptlets

  • 12

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    147 Omega Developing Applications for Java EE Platform

    Declaration Scripting Elements

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    148 Omega Developing Applications for Java EE Platform

    Expression Scripting Elements

  • 13

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    149 Omega Developing Applications for Java EE Platform

    Scriptlet Scripting Elements

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    150 Omega Developing Applications for Java EE Platform

    Scriptlet Scripting Elements

  • 14

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    151 Omega Developing Applications for Java EE Platform

    Scriptlet Scripting Elements

    Translate to

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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.

  • 15

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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 pages contextYou can define additional action types using custom tag libraries.

    Dev

    elop

    ing

    wit

    h JS

    P Te

    chno

    logy

    5

    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

Search related