Session 2
Using JSP Tags
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 2 of 45
Session Objectives
Describe the various elements of
a JavaServer Page
Describe the various JSP Tags in
brief
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 3 of 45
What does a JSP look like?
Three main JSP constructs:
Directives
Allows one to control structure of the servlet
Scripting Elements
Used to include Java code
Actions
Specific tags that affect the runtime behavior of the
JSPs
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 4 of 45
An Example
<HTML>
<HEAD><TITLE>MyFirstProgram.jsp</TITLE></HEAD>
<BODY>
<! MyFirstProgram.JSP >
<%@ page import = "java.util.Date" %>
<% out.println("Hello there!"); %> <Br>
<%= "Current date is " + new Date() %>
</BODY> </HTML>
Fixed Template
data
JSP
Directive
JSP
Expression
JSP
Scriptlet
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 5 of 45
What happens under the hood?
User
Request
Web Server/
Servlet Engine
JSP Handler Servlet
(Page Compilation Servlet)
New or
modified file?
Compile into
Servlet
Execute
Servlet
Yes
No
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 6 of 45
Various Elements in a JSP
Directives
page
Defines information that will be globally available for a
JSP
include
Used to insert text and code at JSP translation time
taglib
Defines a mechanism for extending the current set of
JSP tags
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 7 of 45
Different Attributes of the
Page Directive
Attribute Definition
language =
”<scriptingLanguage>”
This attribute tells the server about the language to be
used to complete the JSP file. At present, Java is the only
language that can be used.
extends =
”<className>"
This attribute defines the parent class that the generated
servlet will extend.
import =
”<importList>"
This attribute defines the list of packages that will be
available to this JSP. However, a comma must separate
each package.
session = "true | false" This attribute determines whether the session data will be
available to this page. The default is true.
buffer = "none | <size
in kb>"
This attribute determines whether the output stream is
buffered. The default value is 8 kb.
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 8 of 45
Different Attributes of the
Page Directive – (1)
Attribute Definition
autoFlush = "true |
false"
Determines whether the output buffer will be flushed
automatically, or whether it will raise an exception
when the buffer is full. The default is true.
isThreadSafe = "true
| false"
Specifies whether page can service more than one
request at a time. The default is true.
info = ”<text>" Specifies information about the JSP, can be accessed
by Servlet.getServletInfo().
errorPage =
”<error_URL>"
This attribute represents the relative URL to the JSP
that will handle exceptions.
isErrorPage = "true |
false"
This attribute states whether or not the JSP is an
errorPage. The default is false.
contentType =
”<ctinfo>"
This attribute represents the MIME type and
character set of the response.
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 9 of 45
Using page Attributes - An
example
<%@ page language = "java"
import = "java.rmi.*, java.util.*"
session = "true"
buffer = "12kb"
autoFlush = "true"
info = "page directive jsp"
errorPage = "Error.jsp"
isErrorPage = "false"
isThreadSafe = "false" %>
<HTML> <TITLE> JSP Elements </TITLE>
<HEAD> <H1> JSP Elements </H1> </HEAD>
<BODY> </BODY> </HTML>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 10 of 45
More on Directives
include directive
Syntax
<%@ include file = “relativeURLspec” %>
taglib directive
Syntax
<%@ taglib uri = “tagLibraryURI”
prefix = “tagPrefix” %>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 11 of 45
Some Important Points
While using directives, remember the
following:
Directives are messages to the JSP container, that is, the
JSP engine
Directives do not store any results in the output buffer
Directives are processed when the JSP is initialized
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 12 of 45
JSP Scripting Elements
Expressions
Scriptlets
Declarations
Scripting is a mechanism for embedding
code fragments directly in an HTML page.
The following three scripting elements are
involved in JSP scripting:
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 13 of 45
JSP Scripting Elements – (1)
Expressions are evaluated to produce a
java.lang.String type result.
Evaluated to
produce 4
<HTML>
<BODY> 2 + 2 equals <%= 2 + 2 %>
</BODY>
</HTML>
Example:
Syntax: <%= expression %>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 14 of 45
JSP Scripting Elements – (2)
Scriptlets contain code that is valid for the
language specified in the language directive. Java
is the only language that is presently available.
Scriptlet
<HTML> <BODY>
<% out.println("HELLO WORLD!!"); %>
</BODY> </HTML>
Example:
Syntax: <% scriptlet_source; %>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 15 of 45
Like expressions, scriptlets also have access to
implicit variables.
JSP Scripting Elements – (3)
<%
String queryData = request.getQueryString();
out.println("Attached GET data: "+queryData);
%>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 16 of 45
Scriptlets do not need complete Java statements, and
open blocks can affect the static HTML outside the
scriptlets.
JSP Scripting Elements – (4)
<% if (Math.random() > 0.5) { %>
Have a <B> happy </B> birthday!!
<% } else {%>
Have a <B> nice </B> day!!
<% } %>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 17 of 45
JSP Scripting Elements – (5)
Declarations are the definition of class-level
variables and methods that are used in a JSP.
Declaration
<%! private static int count_access = 0; %>
This page has been accessed
<%= ++count_access %>
time(s) since the server was rebooted.
Example:
Syntax: <%! Declaration; %>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 18 of 45
Declarations do not produce any printable results.
Variables are initialized when the JSP is
initialized.
Such variables are available to other declarations,
expressions, and scriptlets.
Variables created through declarations become
instance variables. Simultaneous users of the JSP
share the same instance of the variable.
Class variables and functions can be declared in a
declaration block.
JSP Scripting Elements – (6)
Points to remember about declarations:
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 19 of 45
A JSP action directive provides an easy method to
encapsulate common tasks. These typically create or
act on objects, usually JavaBeans.
JSP Standard Actions
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>
<jsp:param>
<jsp:include>
<jsp:forward>
<jsp:plugin>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 20 of 45
JSP Standard Actions – (1)
<jsp:useBean>
Associates an instance of a pre-defined JavaBean with a
given scope and ID
<jsp:setProperty>
Sets the value of a bean’s property
<jsp:getProperty>
Accesses the value of the specified property of a bean
instance
Converts it to a java.lang.String object
Places it in the implicit out object
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 21 of 45
JSP Standard Actions – (2)
<jsp:param>
Is used to provide the tag/value pairs of information
Included as sub-attributes of jsp:include, jsp:forward,
and jsp:plugin actions
The syntax is as follows:
<jsp:param name = "pName" value = "pValue">
</jsp:param>
<jsp:param name = "pName" value = "pValue"/>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 22 of 45
JSP Standard Actions – (3)
<jsp:include>
Provides a mechanism for including additional static
and dynamic resources in the current JSP
The syntax is as follows:
<jsp:include page = "urlSpec" flush = "true"/>
<jsp:include page = "urlSpec" flush = "true">
<jsp:param />
</jsp:include>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 23 of 45
Using <jsp:include> - An
Example
<! Emplheader.jsp >
<! Get employee’s name from the request >
<% out.println("Employee: " +
request.getParameter("employee")); %>
<! Get employee’s title from the request >
<% out.println("Title: " +
request.getParameter("title")); %>
This JSP will search for an employee’s name and title.
It will also act as a header for Employeeinfo.jsp.
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 24 of 45
Using <jsp:include> - An
Example – (1)
<HTML>
<TITLE> Employee Information </TITLE> <BODY>
<jsp:param name= "employee" value= "Martha" />
<jsp:param name= "title" value= "Doctor"/>
</jsp:include>
</BODY> </HTML>
<jsp:include page= "Emplheader.jsp"
flush= "true" >
Employeeinfo.jsp
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 25 of 45
<jsp:include>
Employeeinfo.jsp
Emplheader.jsp
<jsp:include page = “Empl…