JSP Directives
JSP
Directives:
JSP directive elements provide information to
JSP container about the page.
<%@ directive attribute="value" %>
There are three types of directive tags:
Directive
|
Description
|
<%@ page ... %>
|
Defines page-dependent attributes, such as scripting language,
error page, and buffering requirements.
|
<%@ include ... %>
|
Includes a file during the translation phase.
|
<%@ taglib... %>
|
Declares a tag library, containing custom actions, used in the
page
|
JSP page
directive:
Gives high-level information
about the servlet that will result from the JSP page.
Can control:
–Which classes are imported?
–What class the servlet extends
–What MIME type is generated?
–How multithreading is handled
–If the servlet participates in
sessions
–The size and behavior of the
output buffer
–What page handles unexpected errors?
The page directive has the
following form:
<%@ page attributes %>
Some of the important page
attributes are:
•import
•session
•contentType
•errorPage
•isErrorPage
Ex.
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
JSP page
directives in tabular forms:
Attribute
|
Purpose
|
contentType
|
Defines the character encoding scheme.
|
errorPage
|
Defines the URL of another JSP that reports on Java unchecked
runtime exceptions.
|
isErrorPage
|
Indicates if this JSP page is a URL specified by another JSP
page's errorPage attribute.
|
extends
|
Specifies a super class that the generated servletmust extend
|
import
|
Specifies a list of packages or classes for use in the JSP as
the Java import statement does for Java classes.
|
isThreadSafe
|
Defines the threading model for the generated servlet.
|
language
|
Defines the programming language used in the JSP page
|
session
|
Specifies whether or not the JSP page participates in HTTP
sessions
|
JSP Page
directive template:
·
The page directive specifies attributes for the page;
·
All
the attributes are optional, and
·
The
essential ones have default values, shown in bold:
<%@ page language="java“
extends="package.class"
import="package.class,
package.*, ..." session="true|false"
buffer="none|default|sizekb"
autoFlush="true|false"
isThreadSafe="true|false"
info="Sample JSP to show
tags"
isErrorPage="true|false"
errorPage="ErrorPage.jsp"
contentType="TYPE| TYPE;
charset=CHARSET| text/html; charset=ISO-8859-1"
pageEncoding="default"
%>
JSP page
directive –errorPage:
Format
<%@ page
errorPage="Relative URL" %>
Purpose
–Specifies a JSP page that should
process any exceptions thrown but not caught in the current page
Notes:
–The exception thrown will be
automatically available to the designated error page by means of the
"exception “variable
–The error Page attribute is for
page-specific error pages
JSP page
directive –isErrorPage:
Format
<%@ page
isErrorPage="true" %>
<%@ page
isErrorPage="false" %> <%--Default --%>
• Purpose
–Indicates whether or not the
current page can act as the error page for another JSP page
• Notes
–A new predefined variable called
exception is created and is accessible from error pages
–Use this for emergency backup
only; explicitly handle as many exceptions as possible
• Don't forget to always check query
data for missing or malformed values
errorPage&
isErrorPageexample:
sample1.jsp
<%@ page
errorPage="error.jsp" language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Tester</title>
</head>
<body>
<%!
String string= null;
%>
The length of the string is
<%= string.length() %>
</body>
</html>
error.jsp
<%@ page
language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
isErrorPage="true"%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Error
Page</title>
</head>
<body>
<h2>Error
Encountered</h2>
<b><font
color="red"><%= exception %></font> has encountered.
</b>
String variable has null.
</body>
</html>
JSP directive –include:
They include
Directive:
·
They include directive can be used to include the contents of
some other file in a JSP
-EXAMPLE:
<%@ include
file=”../Header.html”%>
·
The contents of the include file will be pasted as a part of the
JSP.
·
The contents can be static (HTML Page) od dynamic (Another JSP).
·
The contents of a page can thus be separated into more
manageable elements.
·
An example of include directive is including a common header and
footer with multiple pages of content.
·
Let us define following three files (a) header.jsp (b)footer.jsp
and (c)main.jsp as follows:
Following is the content of header.jsp
<html>
<head>
<title>The include Directive Example</title> </head>
<body>
<center>
<h2>The include Directive Example</h2>
<p>Triniti</p>
</center>
<br/><br/>
</body>
</html>
main.jsp
<html><body>
<%@ include
file="header.jsp" %>
<%!
intpageCount= 0;
intaddCount() {
pageCount++;
return pageCount;
}
%>
<center>
<p>Thanks for visiting my
page.</p>
<p> Page Count: <%= addCount()
%> </p>
</center>
<%@ include
file="footer.jsp" %>
</body>
</html>
footer.jsp
<html><body>
<br/><br/>
<center>
<p>Copyright mobbiinfo©
2016</p>
</center>
</body></html>
Comments
Post a Comment