JSP
Directives
JSP
directives provide directions and instructions to the container,
telling it how to handle certain aspects of JSP processing.
There are three
directives
- page Directive
- include Directive
- taglib Directive
- page Directive
Defines
attributes that apply to an entire JSP page.
Syntax
<%@ page
[
language="java"
]<%@ page
[extends="package.class" ]
[ import="{package.class|package.*}, ..." ]
[ session="true| false" ]
[ buffer="none |8kb|sizekb" ]
[ autoFlush="true| false" ]
[ isThreadSafe="true| false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType[ ;charset=characterSet]" | "text/html ; charset=ISO-8859-1" ]
[ isErrorPage="true |false" ]
%>
Example :
<%@ page import="java.util.*,java.lang.*" %>
<%@ page buffer="5kb" autoFlush="false" %>
<%@ page errorPage="error.jsp" %>
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.*"
isErrorPage="true"%>
Description
The
<%@
page
%>
directive
applies
to
an
entire
JSP
file
and
any
of
its
static
include
files,
which
together
are
called
a
translation
unit.
A
static
include
file
is
a
file
whose
content
becomes
part
of
the
calling
JSP
file.
The
<%@
page
%>
directive
does
not
apply
to
any
dynamic
include
files.
You
can
use
the
<%@
page
%>
directive
more
than
once
in
a
translation
unit,
but
you
can
only
use
each
attribute,
except
import
,
once.
Because
the
import
attribute
is
similar
to
the
import
statement
in
the
Java
programming
language,
you
can
use
a
<%@
page
%>
directive
with
import
more
than
once
in
a
JSP
file
or
translation
unit.No matter where you position the <%@ page %>
directive
in
a
JSP
file
or
included
files,
it
applies
to
the
entire
translation
unit.
However,
it
is
often
good
programming
style
to
place
it
at
the
top
of
the
JSP
file.Attributes :
The
scripting
language
used
in
scriptlets,
declarations,
and
expressions
in
the
JSP
file
and
any
included
files.
In
this
release,
the
only
allowed
value
is
java
.
The fully
qualified name of the superclass of the Java class file this JSP file
will be compiled to. Use this attribute cautiously, as it can limit
the JSP container's ability to provide a specialized superclass that
improves the quality of the compiled file.
A
comma-separated
list
of
Java
packages
that
the
JSP
file
should
import.
The
packages
(and
their
classes)
are
available
to
scriptlets,
expressions,
and
declarations
within
the
JSP
file.
If
you
want
to
import
more
than
one
package,
you
can
specify
a
comma-separated
list
after
import
or
you
can
use
import
more
than
once
in
a
JSP
file.
The
following
packages
are
implicitly
imported,
so
you
don't
need
to
specify
them
with
the
import
attribute:
Whether
the
client
must
join
an
HTTP
session
in
order
to
use
the
JSP
page.
If
the
value
is
true
,
the
session
object
refers
to
the
current
or
new
session.
If
the
value
is
false
,
you
cannot
use
the
session
object
or
a
<jsp:useBean>
element
with
scope=session
in
the
JSP
file.
Either
of
these
usages
would
cause
a
translation-time
error.
The
buffer
size
in
kilobytes
used
by
the
out
object
to
handle
output
sent
from
the
compiled
JSP
page
to
the
client
Web
browser.
The
default
value
is
8kb
.
If
you
specify
a
buffer
size,
the
output
is
buffered
with
at
least
the
size
you
specified.
Whether
the
buffered
output
should
be
flushed
automatically
when
the
buffer
is
full.
If
set
to
true
(the
default
value),
the
buffer
will
be
flushed.
If
set
to
false
,
an
exception
will
be
raised
when
the
buffer
overflows.
You
cannot
set
autoFlush
to
false
when
buffer
is
set
to
none
.
Whether
thread
safety
is
implemented
in
the
JSP
file.
The
default
value
is
true
,
which
means
that
the
JSP
container
can
send
multiple,
concurrent
client
requests
to
the
JSP
page.
You
must
write
code
in
the
JSP
page
to
synchronize
the
multiple
client
threads.
If
you
use
false
,
the
JSP
container
sends
client
requests
one
at
a
time
to
the
JSP
page.
A
text
string
that
is
incorporated
verbatim
into
the
compiled
JSP
page.
You
can
later
retrieve
the
string
with
the
Servlet.getServletInfo()
method.
A pathname to a
JSP file that this JSP file sends exceptions to. If the pathname
begins with a /, the path is relative to the JSP application's
document root directory and is resolved by the Web server. If not,
the pathname is relative to the current JSP file.
Whether
the
JSP
file
displays
an
error
page.
If
set
to
true
,
you
can
use
the
exception
object
in
the
JSP
file.
If
set
to
false
(the
default
value),
you
cannot
use
the
exception
object
in
the
JSP
file.
The
MIME
type
and
character
encoding
the
JSP
file
uses
for
the
response
it
sends
to
the
client.
You
can
use
any
MIME
type
or
character
set
that
are
valid
for
the
JSP
container.
The
default
MIME
type
is
text/html
,
and
the
default
character
set
is
ISO-8859-1
.
Example
:
<%@page
import="java.text.NumberFormat"%>
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.*"
isErrorPage="false"
isThreadSafe="true"
buffer="10kb"
autoFlush="true"
errorPage="error.jsp"
isELIgnored="false"%>
<!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=UTF-8">
<title>Insert
title
here</title>
</head>
<body>
<%=new
java.util.Date()%>
<%
NumberFormat
number = NumberFormat.getNumberInstance();
number.setMaximumIntegerDigits(5);
String
value = number.format(999999);
%>
<%=value%>
</body>
</html>
2. include
Directive
Includes a static file in a JSP file, parsing the file's JSP elements.
Description
The
<%@
include
%>
directive
inserts
a
file
of
text
or
code
in
a
JSP
file
at
translation
time,
when
the
JSP
file
is
compiled.
When
you
use
the
<%@
include
%>
directive,
the
include
process
is
static.
A
static
include
means
that
the
text
of
the
included
file
is
added
to
the
JSP
file.
The
included
file
can
be
a
JSP
file,
HTML
file,
or
text
file.
If
the
included
file
is
a
JSP
file,
its
JSP
elements
are
translated
and
included
(along
with
any
other
text)
in
the
JSP
file.
Once
the
included
file
is
translated
and
included,
the
translation
process
resumes
with
the
next
line
of
the
including
JSP
file.
The
included
file
can
be
an
HTML
file,
a
JSP
file,
a
text
file,
or
a
code
file
written
in
the
Java
programming
language.
Be
careful
that
the
included
file
does
not
contain
<html>
,</html>
,
<body>
,
or
</body>
tags.
Because
the
entire
content
of
the
included
file
is
added
to
the
including
JSP
file,
these
tags
would
conflict
with
the
same
tags
in
the
including
JSP
file,
causing
an
error.
Some
of
the
behaviors
of
the
<%@
include
%>
directive
depend
on
the
particular
JSP
container
you
are
using,
for
example:- The JSP page might be recompiled if the included file changes.
Example
:
<%@page
import="java.util.Date"%>
<%@page
import="java.text.SimpleDateFormat"%>
<%
Date
d
=
new
Date();
SimpleDateFormat
date
=
new
SimpleDateFormat("dd-MM-yyyy");
String
data = date.format(d);
%>
Today
date
is
:<%=data%>
Thanks
for visting
index.jsp
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isErrorPage="false"
isThreadSafe="true"
buffer="10kb"
autoFlush="true"
errorPage="error.jsp"
isELIgnored="false"%>
<!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=UTF-8">
<title>Insert
title
here</title>
</head>
<body>
<h3>Welcome
to
JSL
Tech</h3>
<center>
<%@include
file="dateformat.jsp"%>
</center>
</body>
</html>
3. taglib Directive
Defines a tag library and prefix for the custom tags used in the JSP page.
Syntax : <%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %>
Examples
- <public:loop>
- -
- </public:loop>
- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
The
<%@
taglib
%>
directive
declares
that
the
JSP
file
uses
custom
tags,
names
the
tag
library
that
defines
them,
and
specifies
their
tag
prefix.
Exception
Handling
We can specify exceptions in JSP using page directive and errorPage attribute.
A jsp page has a default implicit Object named exception and this object is an instance of java.lang.Throwable.
As in servlets, we can handle exceptions in jsp by using try/catch block or by forwarding the request to an error page when an uncaught exception occurs. The exception object is only available to pages that have isErrorPage set to true with the directive <%@ page isErrorPage='true' %>. The exception object refers to the exception triggered by the page that uses this page as an exception handler.
The following code demonstrates the use of the exception implicit object. The first page uses the errorPage directive to set up the JSP page to use when an error occurs, and the second page called ErrorPage.jsp uses the isErrorPage directive to set itself up to catch the error.
Examples
index.jsp
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
isErrorPage="false"
isThreadSafe="true"
buffer="10kb"
autoFlush="true"
errorPage="error.jsp"
isELIgnored="false"%>
<!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=UTF-8">
<title>Insert
title
here</title>
</head>
<body>
<%
int
a
=
100,
b
=
10;
int
res
=
a
/
b;
String
name
=
"Miani";
%>
The
result
is
:<%=res%>
The
name
is
:<%=name.substring(9)%>
</body>
</html>
error.jsp
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"
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=UTF-8">
<title>Insert
title
here</title>
</head>
<body>
<%=exception%>
</body>
</html>
web.xml
We can also handle exception in deployment descriptor file by specifying the error page locations for specific exception-types.
Syntax :
<error-page>
<exception-type> </exception-type>
<location> </location>
</error-page>
No comments:
Post a Comment