JSP Implicit Objects and EL: Name: Kaijie Sheng Personal Number: 830831-5053
JSP Implicit Objects and EL: Name: Kaijie Sheng Personal Number: 830831-5053
3/3/09 2
1. Scope of JSP Objects
3/3/09 3
Definition on Scope of JSP Objects
3/3/09 4
Kinds
Object scope in JSP is segregated into four
parts according to their available rang and life
time。
Page scope
Request scope
Session scope
Application scope
3/3/09 5
Page scope
The JSP object can be accessed only from within
the same page where it was created.
3/3/09 6
Request scope
A JSP object created using the ‘request’ scope can
be accessed from any pages that serves that
request.
3/3/09 7
Session scope
the JSP object is accessible from pages that
belong to the same session from where it was
created.
3/3/09 8
Application scope
A JSP object created using the ‘application’ scope
can be accessed from any pages across the
application.
3/3/09 9
Operation on Attributes
Attribute operation method Annotation
public void setAttribute(String name, Object Set the “name” as the reference of
ob) the object “ob”
public Object getAttribute(String name) Get the value of attribute “name”
3/3/09 11
Overview
JSP Implicit Super Class Annotation
Object
request
JSPHttpServletRequest
2.0中有9个隐含对象 Provides HTTP request information.
response HttpServletResponse Send data back to the client
session HttpSession Track information about a user from one request to another
application ServletContext Data shared by all JSPs and servlets in the
application.
pageContext PageContext Contains data associated with the whole page
config ServletConfig Provides servlet configuration data.
3/3/09 12
Request object
Methods of request Object:
getCookies()
return all cookies sent with the request information by the
client
getHeader(String name)
return the value of the requested header
getHeaderNames()
return all the header names in the request
3/3/09 13
Request object
Methods of request Object:
getAttribute(String name)
return the value of the attribute
getAttributeNames()
return the object associated with the particular given
attribute
getMethod()
return the methods GET, POST, or PUT corresponding to
the requested HTTP method used
3/3/09 14
Request object
Methods of request Object:
getParameter(String name)
return the value of a requested parameter
getParameterNames()
return the names of the parameters given in the current
request
getParameterValues(String name)
return the value of a requested given parameter
3/3/09 15
Request object
Methods of request Object:
getQueryString()
return the query string from the request
getRequestURI()
return the URL of the current JSP page
getServletPath()
return the part of request URL that calls the servlet
3/3/09 16
Request object
Methods of request Object:
setAttribute(String,Object)
used to set object to the named attribute.
removeAttribute(String)
used to remove the object bound with specified name from
the corresponding session
3/3/09 17
Response object
Methods of Response Object:
setContentType()
set the MIME type and character encoding for the page
addCookie(Cookie cookie)
used to add the specified cookie to the response
3/3/09 18
Response object
Methods of Response Object:
containsHeader(String name)
check whether the response already includes the header
given as parameter
sendRedirect(String)
send a redirect response to the client temporarily by
making use of redirect location URL given in parameter
sendError(int status_code)
send an error response to the client containing the
specified status code given in parameter
3/3/09 19
Out object
Methods of Out Object:
Clear()
clear the output buffer
clearBuffer()
clear the output buffer
Flush()
flush the buffer by writing the contents to the client
3/3/09 20
Out object
Methods of Out Object:
isAutoFlush
return a true value if the output buffer is automatically
flushed
getBufferSize
return the size of the buffer
getRemaining
return the number of empty bytes in the buffer
3/3/09 21
Out object
Methods of Out Object:
print
write the value to the output without a newline character
println
write the value to the output, including the newline
character
3/3/09 22
Session object
Methods of Session Object:
getAttribute(String name)
return the object with the specified name given in
parameter
getAttributeNames()
retrieve all attribute names associated with the current
session
isNew()
return a true value if the session is new
3/3/09 23
Session object
Methods of Session Object:
getCreationTime
return the session created time
getLastAccessedTime
return the latest time of the client request associated with
the session
getId
return the unique identifier associated with the session.
3/3/09 24
Session object
Methods of Session Object:
invalidate()
discard the session and releases any objects stored as
attributes
getMaxInactiveInterval ()
return the maximum amount of time the JRun keeps the
session open between client accesses
setMaxInactiveInterval()
set the timeout explicitly for each session
3/3/09 25
Session object
Methods of Session Object:
removeAttribute(String name)
remove the attribute and value from the session
setAttribute(String, object)
set the object to the named attribute
3/3/09 26
Application object
Methods of Application Object:
getAttribute(String name)
return the attribute with the specified name
getAttributeNames
return the attribute names available within the application
3/3/09 27
Application object
Methods of Application Object:
removeAttribute(String objName)
remove the name of the object mentioned in parameter of
this method from the object of the application
getMajorVersion()
return the major version of the Servlet API for the JSP
Container
getMinorVersion()
return the minor version of the Servlet API for the JSP
Container
3/3/09 28
Application object
Methods of Application Object:
getServerInfo()
return the name and version number of the JRun servlet
engine
getInitParameter(String name)
return the value of an initialization parameter
getInitParameterNames()
return the name of each initialization parameter
3/3/09 29
Application object
Methods of Application Object:
getResourceAsStream(Path)
translate the resource URL mentioned as parameter in the
method into an input stream to read
log(Message)
write a text string to the JSP Container’s default log file
3/3/09 30
3. An Example about JSP Object
—— Simple Chat Room
3/3/09 31
3/3/09 32
index.html
<html>
<head>
<title>Index</title>
</head>
<body>
<div align="center">
<center>
<p> </p>
<p><font face="Times New Roman"
size="6"><b>Login</b></font></p>
<table border="2" width="400" bordercolorlight="#FFFFFF"
bordercolordark="#000000" cellspacing="0" cellpadding="0">
3/3/09 33
index.html
<tr>
<td width="100%">
<form method="POST" action="login.jsp"
name="login_form">
<p align="center"> </p>
<p align="center">username:<input type="text"
name="userName" size="20"></p>
<p align="center">password:<input type="password"
name="passwd" size="20"></p>
<p align="center">
<input class="buttonface" type="submit" value="Login"
name="B1">
3/3/09 34
index.html
<input class="buttonface" type="reset" value="cancel"
name="B2"></p>
</form>
</td>
</tr>
</table>
</center>
</div>
</body>
</html>
3/3/09 35
3/3/09 36
login.jsp
<%@ page session="true" contentType="text/html;charset=GBK"
%>
<%
//init session as false
session.setAttribute("login","false");
else{
// both username and password are correct
if (userName.equals("kaijie")&&passwd.equals("123")){
session.setAttribute("login","true");//set attribute "login" as
true in session
session.setAttribute("userName",userName);
%>
3/3/09 38
login.jsp
<h2>Login successfully!</h2>
<a href="chat.jsp">Go to chat room</a>
<%
}//if
else{
response.sendRedirect("./index.html");
}
}//else
%>
3/3/09 39
3/3/09 40
chat.jsp
<%@ page contentType="text/html;charset=GBK"
import="java.util.*"%>
<html>
<head>
<title>simple chat room</title>
</head>
<body>
<center>
<%
if (request.getProtocol().compareTo("HTTP/1.0") == 0)
response.setHeader("Pragma", "no-cache");
else if (request.getProtocol().compareTo("HTTP/1.1") == 0)
response.setHeader("Cache-Control", "no-cache");
3/3/09 41
chat.jsp
response.setDateHeader("Expires", -1);
response.setHeader("Refresh","10");
//use request object get input chat data from the input textfield
String curChat = request.getParameter("inputChatTextField");
3/3/09 42
chat.jsp
//use application object store the chat data
String chatRecord = (String)application.getAttribute("chatRecord");
3/3/09 43
chat.jsp
//show char data within a table
if (chatRecord!=null){
application.setAttribute("chatRecord", chatRecord); %>
<table border="2" width="400" bordercolorlight="#FFFFFF"
bordercolordark="#000000" cellspacing="0" cellpadding="0">
<tr>
<th>simple chat room</th>
</tr>
<tr>
<td width="100%"><%=
application.getAttribute("chatRecord")%></td>
</tr>
</table>
<%}%>
3/3/09 44
chat.jsp
<FORM ACTION="chat.jsp" METHOD="post">
<p><INPUT TYPE="TEXT" SIZE="30" NAME="inputChatTextField"
VALUE="">
<INPUT TYPE="SUBMIT" name="submit" VALUE="send"></p>
</FORM>
</center>
</BODY>
</HTML>
3/3/09 45
4. Expression Language (EL)
3/3/09 46
Expression Language (EL) is used to
extend the “coding capability” of a JSP,
thereby reducing the requirement for
Java-scriptlets.
3/3/09 47
Lane Syntax
${expr}
expr stands for a valid expression
Valid Expression:
Literals
Operators
Variables (object references)
Function call
3/3/09 48
Literals
Literals Literal values
Boolean true and false
Integer Any positive or negative number
e.g, 24, -45, 567
Floating Any positive or negative floating point numbers
e.g, -1.8E-45, 4.567
String Any string delimited by single or double quotes.
Null null
3/3/09 49
Operator
Term Definition
Arithmetic +, - (binary), *, /, div, %, mod, - (unary)
Logical and, &&, or, ||, !, not
Relational ==, eq, !=, ne, <, lt, >, gt, <=, le, >=
Empty used to determine if a value is null or empty.
3/3/09 50
Implicit Object
Term Definition
pageContext used to access the JSP implicit objects such as request,
response, session, out, servletContext etc.
${pageContext.response} evaluates to the response object for
the page.
param maps a request parameter name to a single String parameter
value,
$(param.name) is equivalent to request.getParameter (name).
paramValues maps a request parameter name to an array of values,
${paramvalues.name) is equivalent to
request.getParamterValues(name).
header maps a request header name to a single String header value.
${header.name} is equivalent to request.getHeader(name).
headerValues maps a request header name to an array of values .
${headerValues.name} is equivalent to
request.getHeaderValues(name).
3/3/09 51
Implicit Object
Term Definition
cookie maps cookie names to a single cookie object
initParam maps a context initialization parameter name to a
single value
pageScope maps page-scoped variable names to their values
requestScope maps request-scoped variable names to their values
sessionScope maps session-scoped variable names to their values
3/3/09 52
Thank you very much !
3/3/09 53