Anatomy of a JSP

A JSP consists of two basic items: template data and JSP elements. Template data provides the static aspects, and JSP elements are used for the dynamic aspects of a JSP.

Template Data

Template data refers to the static HTML or XML content of the JSP. Although it is essential for the JSP presentation, it is relatively uninteresting from a JSP programming point of view.

Aside from the usual substitutions, such as those based on quoting and escape sequences, the template data is written verbatim as part of the JSP response.

JSP Elements

JSP elements represent the portion of the JSP that gets translated and compiled into a servlet by the JSP compiler. In syntax, JSP elements are similar to HTML elements in that they have a begin and an end tag (for example, <B> bold text </B>).

There are three types of JSP elements defined in the JSP specification: directive elements, action elements, and scripting elements.

Directive Elements

Directive elements provide global information for the translation phase. These directives are general in nature, that is, not related to a specific request and thus do not directly impact the output to the client.

Directive elements take the following form:

<% @directive-name directive-attribute="attribute-value" other-
attribute-value-pairs ... %>

An example of a directive element follows:

<% include file="Header.jsp" %>

A page directive and its attributes provide a convenient mechanism for instructing the environment on the configuration of various things, such as libraries to be imported, content type of the page, buffer size, and so on. With the exception of the import attribute, other page attributes can only be defined once in the JSP.

Action Elements

Unlike directive elements, action elements come into play during the request-processing phase. JSP actions elements are written using an XML syntax in one of the following two formats:

<prefix:tag attribute=value attribute-value-list.../>

or

<prefix: tag attribute=value attribute-value-list> body
</prefix:tag>

The idea is to establish an association between tags and have a “tag handler” defined for each tag, which gets invoked to handle the tag when the tag is encountered. Tag handlers are essentially pieces of code, for example:

<jsp:forward page="/errorPage" />

Actions prefixed with “jsp” are standard actions. Some standard actions are

  • Include responses sent by other JSPs

  • Forward requests to others

  • Query and update properties of a JavaBean residing on the server

Actions may create objects that are made available to scripting elements via certain variables.

Scripting Elements

Scripting elements bring everything together in a JSP. These elements can be declarations used for defining variables and methods, blocks of code called scriptlets, and expressions for evaluation during request processing.

Declarations

Declarations define variables and methods. The syntax for declarations is <%! Declaration %> where declaration can be a variable or a function, for example:

<%! private static MyLoginCount=0;   %>

Expressions

Expressions are evaluated during the request-processing phase of the JSP, and the results are converted to a string and intermixed with the template data. The result is placed in the same place where the expression was located in the JSP page.

The syntax of expressions is <% = Some expression %>.

In the XML syntax, the same is expressed as:

<jsp:expression>Some expression</jsp:expression>

For example:

Login Count: <%= results %>

Scriptlets

A scriptlet is a mini “script” of code embedded within the JSP. It can contain, among other things, declaration of variables and methods, expressions, and statements. Like expressions, scriptlets get executed at request-processing time, and any resulting output is placed in the response object.

The syntax for declaring scriptlets is <% Java Code %>.

The XML equivalent is

<jsp:scriptlet>Java Code</jsp:scriptlet>

For example:

:
<% int guessNum = request.getParameter("GUESS");
if (guessNum == WinningNum) { %/>
Congrats, you win!!!
<% }
else
{  %/>
Sorry, try again.
<% }  %/>
:

Objects Accessible to a JSP Implicitly

Each JSP has access to some objects without explicitly declaring them in the JSP. These objects are created by the container for use within JSPs and can be assumed to exist by the JSP developers.

These implicit objects are

  • request: Represents the incoming request that initiated the processing

  • response: Represents the response to the current request

  • pageContext: Provides access to page attributes and convenience methods

  • session: Session object for the current client

  • application: Identifies the associated ServletContext

  • out: Object for writing to the output stream

  • config: Identifies the associated servlet config for the JSP

  • page: Similar to this in the context of the current JSP

  • exception: Identifies the exception that led to the error page

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.24.171