SCWCD : JSP directives
Write JSP code that uses the directives: (a) 'page' (with attributes
'import', 'session',
'contentType', and 'isELIgnored'), (b)
'include', and (c) 'taglib'.
page Directive with
import attribute.
Lets you specify the packages that should be imported by the servlet into which the JSP
page gets translated. Generates import statements at top of
servlet definition.
Default imports: java.lang.*; javax.servlet.*;
javax.servlet.jsp.*; javax.servlet.http.*.
<%@ page import="package.class" %>
or multiple classes/packages (separated by comma, NOT semicolon).
<%@ page import="package.classA, package.classB, other.package.*" %>
page Directive with
session attribute.
Controls whether or not page participates in HTTP sessions. Indicates that
session (of type HttpSession) should be bound
to existing session.
false value means NO sessions will be used automatically.
Attempts to access session variable by servlet will cause RUN-TIME failure.
By default, it IS part of a session. All related pages have to do this for it to be useful.
<%@ page session="true" %> <%-- default !!! --%>
or
<%@ page session="false" %>
page Directive with
contentType attribute.
Specify the MIME type of the page generated by the servlet that results from the JSP page.
Attribute value cannot be computed at request time.
<%@ page contentType="MIME-Type" %>
or
<%@ page contentType="MIME-Type; charset=Character-Set" %>
it is the same as (scriptlet):
<% response.setContentType("MIME-Type; charset=Character-Set"); %>
Note, you CANNOT use the contentType attribute for
conditional run-time task.
The following ALWAYS results in the Excel MIME type (page
directive is evaluated only once during translation phase, and not during execution
phase):
<% boolean usingExcel = checkUserRequest(request); %>
<% if (usingExcel) { %>
<%@ page contentType="application/vnd.ms-excel" %>
<% } %>
the following approach should be used instead :
<% boolean usingExcel = checkUserRequest(request); %>
<% if (usingExcel) { %>
<% response.setContentType("application/vnd.ms-excel"); %>
<% } %>
page Directive with
isELIgnored attribute.
The attribute is used to control whether the JSP 2.0 Expression Language (EL) is
ignored (true) or evaluated normally (false).
EL expressions will be ignored by default in JSP 1.2 applications. When upgrading
a web application to JSP 2.0, EL expressions WILL BE INTERPRETED by default. The escape
sequence '$' can be used to escape EL expressions that should not be interpreted by the
container. Alternatively, the isELIgnored page
directive attribute,
or the <el-ignored> configuration element can be used to
deactivate EL for entire translation units.
<%@ page isELIgnored="false" %> <!-- default for JSP 2.0 -->
or
<%@ page isELIgnored="true" %> <!-- default for JSP 1.2 -->
include Directive.
Lets you insert a file into servlet class at time the JSP file is translated into servlet.
Should be placed in document at point where you want file inserted.
The include directive lets you reuse navigation bars, tables,
and other elements in multiple pages. The include directive includes
a file in a JSP document at DOCUMENT TRANSLATION TIME. Included file can contain JSP code.
Inclusion is recursive: included files may include files.
<html>
<head>
<title>Including Files at Translation Time (JSP)</title>
</head>
<body>
<%@ include file="somePage.jsp" %>
</body>
</html>
taglib Directive.
Can be used to define custom tags.
<%@ taglib prefix="example" uri="http://www.server.com/example-taglib" %>
and web.xml:
<taglib>
<taglib-uri>http://www.server.com/example-taglib</taglib-uri>
<taglib-location>/WEB-INF/example-taglib.tld</taglib-location>
</taglib>
Notice the taglib-location specifies the location of the TLD. The
taglib-uri is, for the most part, an arbitrary name given to the
tag library. The name you give it can't conflict with other tag libraries in your
deployment descriptor. In fact, adding the taglib element to the
deployment descriptor is actually optional. You could instead reference the TLD
directly in the taglib directive:
<%@ taglib prefix="example" uri="/WEB-INF/example-taglib.tld" %>
This isn't recommended because it reduces flexibility if you ever choose to rename or
move the TLD. The uri would have to be changed in every JSP that used it.
|