Different Attributes of the Page Directive language = ”” This attribute tells the server about the language to be used to complete the JSP file.. session = "true | false" This attribu
Trang 1Session 2
Using JSP Tags
Trang 3What does a JSP look like?
Three main JSP constructs:
Trang 4<%@ page import = "java.util.Date" %>
<% out.println( "Hello there!" ); %> <Br>
Fixed Template
data
JSP Directive
JSP Expression
JSP Scriptlet
Trang 5What happens under the hood?
Compile into Servlet
Execute Servlet
Yes
No
Trang 7Different Attributes of the
Page Directive
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.
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.
Trang 8Different 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 = This attribute represents the MIME type and
Trang 9Using page Attributes - An
example
<%@ page language = "java"
import = "java.rmi.*, java.util.*"
<HTML> <TITLE> JSP Elements </TITLE>
<HEAD> <H1> JSP Elements </H1> </HEAD>
<BODY> </BODY> </HTML>
Trang 11Some 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
Trang 15Like expressions, scriptlets also have access to implicit variables.
JSP Scripting Elements – (3)
<%
out.println( "Attached GET data: " +queryData );
%>
Trang 16Scriptlets do not need complete Java statements, and open blocks can affect the static HTML outside the scriptlets.
Trang 18 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:
Trang 19A JSP action directive provides an easy method to encapsulate common tasks These typically create or act on objects, usually JavaBeans.
Trang 20JSP Standard Actions – (1)
<jsp:useBean>
Associates an instance of a pre-defined JavaBean with
a given scope and ID
Trang 21 The syntax is as follows:
<jsp:param name = "pName" value = "pValue">
</jsp:param>
<jsp:param name = "pName" value = "pValue"/>
Trang 22JSP 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 />
Trang 23This JSP will search for an employee’s name and title
It will also act as a header for Employeeinfo.jsp.
Trang 24Using <jsp:include> - An
Example – (1)
<HTML>
<TITLE> Employee Information </TITLE> <BODY>
<jsp:param name = "employee" value = "Martha" />
<jsp:param name = "title" value = "Doctor" />
Trang 26JSP Standard Actions – (4)
<jsp:forward>
Enables the JSP engine to dispatch the current request
to a static resource, a servlet, or to another JSP at time
run- The syntax is as follows:
<jsp:forward page = "relativeURLSpec" />
<jsp:forward page = "relativeURLSpec" >
<jsp:param />
Trang 27Using <jsp:forward> - An
Example s
<HTML>
<TITLE> Using the JSP Forward action </TITLE> <BODY>
<! Checks whether the "company id" is equal to 1 >
<% if
((request.getParameter(" companyID")) equals ( "1" )) { %>
<jsp:param name = "employee" value = "Martha" />
<jsp:param name = "title" value = "Doctor" />
</jsp:forward>
<% } else {
out.println( "Sorry, no matching values found" ); } %>
<jsp:forward page = "MarthaHome.jsp" >
Forward.jsp
Trang 30JSP Standard Actions – (5)
Generates HTML that contains the appropriate browser dependent constructs, such as OBJECT or EMBED This will prompt the download of the required Java plugin, and subsequent execution of the applet or bean.
client- The syntax is as follows:
<jsp:plugin type = "pluginType"
code = "classfilename"
codebase = "relativeURLpath" >
<jsp:params> <jsp:param …/>
Trang 31Using <jsp:plugin> - An Example
public class HelloApplet extends JApplet {
public HelloApplet() { }
public void init() { }
public void paint( Graphics g) {
Font f = new Font ("TimesRoman", Font BOLD,12);
Trang 32Using <jsp:plugin> - An Example -
Trang 33 JSP actions have the following characteristics:
server pages
JSP Standard Actions – (6)
Trang 34Comments and Character
<! > An HTML comment that is passed through to the resultant
HTML Any embedded JSP codes are executed normally
< \ % This is used in template text (static HTML) in place of "<%".
% \ > This is used in scripting elements in place of "%>".
\’ A single quote in an attribute that uses single quotes
\" A double quote in an attribute that uses double quotes.
% \ > %> in an attribute.
< \ % <% in an attribute.
Trang 35 These objects determine:
how to accept the request from the browser
how to send the response from the server
how session tracking can be done
Trang 36jsp.JspWriter This is the JspWriter object to
the output stream
Page
Trang 37Implicit Objects – (2)
the this object
for the current instance of the JavaServer page
ServletRequest or javax.servlet.http.
HttpServletRequest
This is the request object that triggered the request
Request
Trang 38Implicit Objects – (3)
subtype of either javax.servlet.Servlet Response or
javax.servlet.http.
HttpServletResponse
This represents the response object triggered
by the request
Page
object, if any, created for the client during an HTTP request
Session
Trang 39Implicit Objects – (4)
of class java.lang.
Throwable It represents the runtime exception that resulted in a call to the error page
Page
Trang 40Using Implicit Objects - An
Trang 41<LI> <B> Declaration - </B> <BR>
<%! private int accessCount = 0; %>
This page has been accessed <%= ++accessCount %>
time(s) since the server was last restarted
<LI> <B> Directive - </B> <BR>
<%@ page import = "java.util.*" %>
Today's Date is: <% = new Date() %>