JavaServer Pages (JSP) is the Java 2 Platform
Enterprise Edition (J2EE) technology for building applications that generate
dynamic web content, such as the HTML, DHTML, XHTML, and XML. JSP technology
enables the authoring of web pages that create dynamic content with maximum
power and flexibility.
JSP 2.0
JSP 2.0 is an upgrade to JSP 1.2 with several
new and interesting features that make the life of Web application designers
and developers easier. The objective of JSP 2.0 is to make JSP easier to
use than ever, and more importantly to be used without having to learn the
Java programming language itself. With JSP 2.0, there are additional properties
that can be specified to configure JSP's in a Web Application.
JSP Configuration
JSP pages may be extended with configuration information
that is delivered in the JSP configuration portion of the web.xml
deployment description of the web application. The information is
described through the jsp-config element and its subelements.
The JSP configuration information includes interpretation for the tag libraries
used in the JSP files and different property information for groups of JSP
files.
The jsp-config element is a subelement of web-app
that is used to provide global configuration information for the JSP files
in a web application. A jsp-config has two subelements: taglib
and jsp-property-group, defining the taglib mapping and groups of
JSP files respectively.
In this How-To document we will be demonstrating
the JSP Configuration using the JSP Property Groups
JSP Property Groups
A JSP property group is a collection of properties that
apply to a set of files that represent the JSP pages. These properties are
defined in one or more jsp-property-group elements in the web
application deployment descriptor.
The properties that can be described in a jsp-property-group
include:
URL Pattern
Control disabling of EL evaluation.
Control disabling of Scripting elements.
Indicating page Encoding information.
Prelude and Coda automatic includes.
Indicating that a resource is a JSP document.
URL Pattern
The applicability of a JSP property group is defined through one or more
URL specification); all the properties in the group apply to the resources
in the web application that match any of the URL patterns. If a resource
matches a URL pattern in both a <servlet-mapping> and
a <jsp-property-group>, the pattern that is most specific
applies. If the URL patterns are identical, the <jsp-propertygroup>
takes precedence over the <servlet-mapping>. If at least
one <jsp-property-group> contains the most specific matching
URL pattern, the resource is considered to be a JSP file, and the properties
in that <jsp-property-group> apply. There is an implicit
property that of being a JSP file.
Deactivating EL Evaluation
Since the syntactic pattern ${expr} that identifies an EL expression
was not reserved in the JSP specifications before JSP 2.0, there may be
situations where such a pattern appears but the intention is not to activate
EL expression evaluation but rather to pass through the pattern verbatim.
Each JSP page has a default mode for EL expression evaluation. The default
value varies depending on the version of the Web application deployment
descriptor.
Disabling Scripting Elements
With the addition of the EL that does away with the usage of scripting,
some JSP page authors, or page authoring groups, may want to follow a methodology
where scripting elements are not allowed. Previous versions of JSP enabled
this through the notion of a TagLibraryValidator that would
verify that the elements are not present. JSP 2.0 makes this slightly easier
through a JSP configuration element.
Declaring Page Encodings
The JSP configuration element page-encoding can be used to set the pageEncoding
property of a group of JSP pages defined using the jsp-property-group
element. The valid values are the same as the pageEncoding
attribute of the JSP page directive. A translation-time error
results if you define the page encoding of a JSP page with one value in
the JSP configuration element and then give it a different value in a pageEncoding
directive. It is also a translation-time error to name different encodings
in the prolog or text declaration of a document in XML syntax and in a JSP
configuration element matching the document. It is legal to name the same
encoding through multiple mechanisms.
Defining Implicit Includes
There are many mechanisms for reusing JSP content in a JSP page. One of
the mechanism that can be categorized as direct reuse is the preludes and
codas. You can implicitly include preludes and codas for a group of JSP
pages by using the include-prelude and include-coda
elements in the jsp-property-group. Their values are context-relative
paths that must correspond to elements in the Web application. When the
elements are present, the given paths are automatically included (as in
an include directive) at the beginning and end of each JSP page in the property
group respectively. When there is more than one include or coda element
in a group, they are included in the same order as they appear in JSP configuration
section in web.xml.
Indicating that a resource is a JSP document
The JSP configuration element is-xml can be used to denote
that a group of files are JSP documents, and thus must be interpreted as
XML documents.
We will see how the above properties are configured
using jsp-property-group in web.xml through a set of example JSP's
that are described later in this document.
To demonstrate the key configurations
using the web.xml, we will consider an example web.xml
that is part of the howtojspconfig.ear file. The complete source for this
can be viewed here. Throughout this document this
example web.xml will be referred to as web.xml,
and based on it the example JSP's are configured.
The web.xml comprises
of four jsp-property-groups definition in it. The applicability
of a JSP property group is defined through url-pattern element.
All the properties in the group apply to the resources in the web application
that matches the URL pattern. The purpose of each of the property group is
as follows:
- The first property group describes the JSP configuration
properties that are applicable to all the JSP's in the application
- The second property group describes the properties
that are applicable to the JSP's under the
jsps-one directory
- The second property group describes the properties
that are applicable to the JSP's under the
jsps-two directory
- The forth property group indicates to the web
container that all the files with
svg extension need to be
treated as JSP documents
Note: A set of example JSP's are provided along with this document
to understand JSP configuration. These JSP files are bundled as an EAR file.
To run the example JSP's, you need to deploy the howtojspconfig.ear
file in OC4J. For doing this, please follow the instructions provided in
the "Deploying the example JSP's in OC4J "
section below. Once the EAR file is deployed, you can proceed to the later
sections on setting of JSP configuration properties using web.xml.
URL Pattern
As mentioned above, the applicability of a JSP property
group can be defined through the url-pattern element of the <jsp-property-group>.
For example, the following web.xml fragment defines a property
group. The url-pattern defined in it indicates that the properties
defined in this property group are applicable for all the JSP files in the
application:
<jsp-property-group> ...
<url-pattern>*.jsp</url-pattern> ... </jsp-property-group>
|
For example, the following web.xml fragment
defines a property group. The url-pattern defined in it indicates
that the properties defined in this property group is applicable for all the
JSP files in the /jsps-one/ directory.
<jsp-property-group> ...
<url-pattern>/jsps-one/*.jsp</url-pattern> ... </jsp-property-group>
|
For example, the following web.xml fragment
defines a property group. The url-pattern defined in it indicates
that the properties defined in this property group is applicable for all the
JSP files in the /jsps-two/ directory.
<jsp-property-group> ...
<url-pattern>/jsps-two/*.jsp</url-pattern> ... </jsp-property-group>
|
Deactivating EL
Evaluation:
The default mode for JSP pages delivered using a Servlet 2.3 or earlier descriptor
is to ignore EL expressions; this provides backwards compatibility. The default
mode for JSP pages delivered with a Servlet 2.4 descriptor is to evaluate
EL expressions. Setting the value of the el-ignored element can explicitly
change the default mode. The el-ignored element is a subelement
of jsp-property-group. It has
no subelements. Its valid values are true and false.
JSP page authors can override the default mode through the isELIgnored
attribute of the page directive. For tag files, there is no default, but the
isELIgnored attribute of the tag directive can be used to control
the EL evaluation settings. Table 1.1 summarizes the EL evaluation
settings for JSP pages and their meanings:
Table 1.1 EL Evaluation Settings for JSP Pages
JSP Configuration
<el-ignored> |
Page Directive
isELIgnored |
EL Encountered |
| Unspecified |
Unspecified |
Ignored if <= 2.3 web.xml
Evaluated otherwise (i.e. if 2.4 web.xml). |
false |
Unspecified |
Evaluated |
true |
Unspecified |
Ignored |
| dont care |
false |
Evaluated |
| dont care |
true |
Ignored |
For example, the following web.xml fragment
defines a property group. This group indicates that the EL should not be ignored
for all the JSP's in the web application by setting the el-ignored
element value to false. Thus in any of the JSP pages in the group
if any scriptlet is encountered it will not be ignored and will be evaluated.
<jsp-property-group> ... <url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored> ... </jsp-property-group>
|
Example JSP:
You can see that the EL expression gets evaluated on running the example-el.jsp
in the web browser. This JSP contains the following EL expression:
To access this JSP in the browser point to the following
URL :
http://<hostname>:<port>/howtojspconfig/jsps-one/example-el.jsp
Disabling Scripting Elements
You can invalidate scripting for a group of JSP pages with scripting-invalid
element which is a subelement of jsp-property-group.
It has no subelements. Its valid values are true and false.
Scripting is enabled by default. Setting the scripting-invalid element to
true in the JSP configuration can do disabling scripting elements. Table
1.2 summarizes the scripting settings and their meanings:
Table 1.2 Scripting Settings
JSP Configuration
<scripting-invalid> |
Scripting Encountered |
| Unspecified |
Valid |
false |
Valid |
true |
Translation Error |
For example, the following web.xml fragment
defines a property group. This group indicates that the scripting is valid
for all the JSP's in the web application
by setting the scripting-invalid element value to false.
Thus in any of the JSP pages in the group if any scriptlet is encountered
it will not be ignored and will be processed. Otherwise when scripting is
invalid, scriptlets, scripting expressions, and declarations will produce
a translation error if present in any of the pages in the group.
<jsp-property-group> ... <url-pattern>*.jsp</url-pattern>
<scripting-invalid>false</scripting-invalid> ... </jsp-property-group>
|
Example JSP:
You can see that the java scriptlet is processed by the container on running
the example-script.jsp.
This JSP contains a java scriptlet to return today's date in the browser.
<%= new java.util.Date() %>
|
To access this JSP in the browser point to the following
URL :
http://<hostname>:<port>/howtojspconfig/jsps-one/example-script.jsp
Declaring Page Encodings
You set the page encoding of a group of JSP pages by using the page-encoding
element of jsp-property-group.
The page-encoding element has no subelements. Its valid values
are those of the JSP pageEncoding page directive.
For example, the following web.xml fragment
defines a property group. This group indicates that the page encoding for
all the JSP's in the web application is set by setting the page-encoding
element value to ISO-8859-1.
<jsp-property-group> ... <url-pattern>*.jsp</url-pattern>
<page-encoding>ISO-8859-1</page-encoding> ... </jsp-property-group>
|
Example JSP:
The JSP example-encode.jsp is a simple JSP that uses the ISO-8859-1
encoding. This JSP can be viewed in the web browser by pointing to the following
URL :
http://<hostname>:<port>/howtojspconfig/jsps-one/example-encode.jsp
It is a translation-time error to name different encodings
in the pageEncoding attribute of the page directive
of a JSP page and in a JSP configuration element matching the page. To verify
this let us consider example-encode-err.jsp that contains JSP
pageEncoding directive pointing to a different encoding(UTF-8)
compared to the one defined in the web.xml.
<%@ page pageEncoding="UTF-8" %>
|
When this JSP is executed in the web browser a translation
error is thrown. This can be tested by running this JSP by pointing the browser
to the following URL :
http://<hostname>:<port>/howtojspconfig/jsps-one/example-encode-err.jsp
Defining Implicit Includes
You can implicitly include preludes and codas for a group of JSP pages by
adding items to the Include Preludes and Codas lists. The include-prelude
element is a subelement of jsp-property-group.
It has no subelements. Similarly the include-coda element is
a subelement of jsp-property-group.
It has no subelements. Preludes and codas follow the same rules as statically
included JSP segments. Preludes and codas can only put the included code at
the beginning and end of each file.
For example, the following web.xml fragment
defines two groups. They indicate that .jsp files in directory
/jsps-one/ to have /common-jsps/prelude1.jspf
at the beginning and /common-jsps/coda1.jspf at the end, while
.jsp files in directory /jsps-two/ to have /common-jsps/prelude2.jspf
at the beginning and /common-jsps/coda2.jspf at the end.
<jsp-property-group> <description>JSP configuration of all the JSP's under /jsps-one/ folder</description> <url-pattern>/jsps-one/*.jsp</url-pattern> <include-prelude>/common-jsps/prelude1.jspf</include-prelude> <include-coda>/common-jsps/coda1.jspf</include-coda> </jsp-property-group> <jsp-property-group> <description>JSP configuration of all the JSP's under /jsps-two/ folder</description> <url-pattern>/jsps-two/*.jsp</url-pattern> <include-prelude>/common-jsps/prelude2.jspf</include-prelude> <include-coda>/common-jsps/coda2.jspf</include-coda> </jsp-property-group>
|
Example JSP:
You can view the prelude and coda inclusion in the JSP's under the /jsps-one/
directory by running the example-preludecoda.jsp in the web browser
by pointing to the following URL :
http://<hostname>:<port>/howtojspconfig/jsps-one/example-preludecoda.jsp
You can view the prelude and coda inclusion in the JSP's
under the /jspstwo/ directory by running the sample-newpreludecoda.jsp
in the web browser by pointing to the following URL :
http://<hostname>:<port>/howtojspconfig/jsps-two/sample-newpreludecoda.jsp
On viewing the above two JSP's you can see that different JSP fragments get
included as prelude and coda based on the location of the JSP files.
Indicating
that a resource is a JSP document (XML)
The is-xml element is a subelement of jsp-property-group.
It has no subelements. Its valid values are true and false.
If true, denotes that the group of resources that match the URL
pattern are JSP documents, and thus must be interpreted as XML documents.
If false, the resources are assumed to not be JSP documents,
unless there is another property group that indicates otherwise. The files
are still considered to be JSP pages due to the implicit property given by
the jsp-property-group element.
For example, the following web.xml fragment
defines a group that indicates that files with extension .svg are
actually JSP documents (which most likely are generating SVG files).
<jsp-property-group> <url-pattern>*.svg</url-pattern> <is-xml>true</is-xml> </jsp-property-group>
|
Deploying the example JSPs in OC4J
The example JSP's can be deployed using the EAR
deployment method as follows:
EAR Deployment
1. Make sure that you've downloaded and installed
OC4J Release 10g.
2. Download the howtojspconfig.ear
into the <OC4J_HOME>\j2ee\home\applications
directory.
3. Go to the <OC4J_HOME>\j2ee\home\config
directory and edit the http-web-site.xml
file.
Add the following entry within the <web-site>
tags :
<web-app
application="howtojspconfig" name="howtojspconfig-web" root="/technology/howtojspconfig" />
Save the file.
4. Go to the <OC4J_HOME>\j2ee\home\config
directory and edit the server.xml
file.
Add the following entry within the <application-server>
tag:
<application
name="howtojspconfig" path="../applications/howtojspconfig.ear" />
Save the file.
5. OC4J's hot-deploy feature will automatically auto-deploy
and install the application for you if the server is running already.
If the server is not running, please start the server and it will deploy
the application automatically at start-up.