A JSP element may refer to several pre-declared implicit objects. These objects are classified according to their scope.
Objects with application scope persist as long as the server is running and are available to all servlets and JSP pages.
ServletContext application; // the container
A session refers to a sequence of requests from the same client and that are part of the same transaction. A session ends when
1. It has been inactive for a period of time
2. The session has been invalidated by the programmer
3. The client closes his/her browser
Objects with session scope persist for the duration of a client's session. They are available only to servlets and JSP pages that participate in the session.
HttpSession session; // client's session data
Several servlets or JSP pages may contribute to the response to a single request (through forward and include directives). Objects with request scope are visible to only these pages.
HttpServletRequest request; // client's request
Although several pages and servlets may contribute to a single response, objects with page scope are only visible to a single page.
HttpServletResponse response; // response to client
JspWriter out; // outputs text to response
PageContext pageContext; // provides access to all implicit objects
Throwable exception; // passed to the error page
The most important thing about the application, session, and request objects is that each provides an attribute map. The following methods can be used to manipulate the attribute map:
Object getAttribute(String name);
void setAttribute(String name, Object val);
void removeAttribute(String name);
Enumeration getAttributeNames();
Programmers use these maps to store data. Of course data stored in the application attribute map will have application scope, etc.
<%!
class CounterBean implements java.io.Serializable {
private int count = 0;
public CounterBean(int n) { count =
n; }
public CounterBean() { count = 0; }
public void inc() { count++; }
public int getCount() { return
count; }
public String toString() { return
"" + count; }
}
%>
<html>
<head> <title> Counters </title> </head>
<body>
<% CounterBean hitCount;
hitCount = (CounterBean)application.getAttribute("hit
count");
if (hitCount == null) {
hitCount = new CounterBean();
application.setAttribute("hit
count", hitCount);
}
hitCount.inc();
%>
Hit count = <%= hitCount %> <br/>
<% CounterBean sessionCount;
sessionCount = (CounterBean)session.getAttribute("session
count");
if (sessionCount == null) {
sessionCount = new CounterBean();
session.setAttribute("session
count", sessionCount);
}
sessionCount.inc();
%>
Session count = <%= sessionCount %> <br/>
</body>
</html>
Try experimenting with this page using several browser sessions simultaneously.
class CounterBean
implements java.io.Serializable, HttpSessionBindingListener
{
private int count = 0;
public CounterBean(int n) { count = n;
}
public CounterBean() { count = 0; }
public void inc() { count++; }
public int getCount() { return count; }
public String toString() { return
"" + count; }
// automatically called when this
counter is bound to a session
public void
valueBound(HttpSessionBindingEvent e) {
System.out.println(e.getName() +
" bound");
}
// automatically called when this
counter
// is unbound from a session
public void
valueUnbound(HttpSessionBindingEvent e) {
System.out.println(e.getName() +
" unbound");
}
}
Add the following to the end of Counter.jsp body:
Session created at
<%= new
java.util.Date(session.getCreationTime()) %> <br />
Session last accessed at
<%= new java.util.Date(session.getLastAccessedTime()) %>
<br />
<% long timeOut = 25; // < 0 indicates no expiration
session.setMaxInactiveInterval(timeOut);
%>
Session max inactive interval is
<%= session.getMaxInactiveInterval()
+ " secs" %> <br />
<% if (sessionCount.getCount() > 5) session.invalidate(); %>
User info form: <hr />
<form action = "/myapp/request.jsp" >
<!-- Fields -->
<p>
<input type = "hidden"
name = "arg" value = "42">
Name: <input type =
"text" name = "client" value = "???" size =
"30"/> <br />
Password: <input type =
"password" name = "password" size = "8"/>
</p>
<!-- Radio buttons -->
<p>
Party: <br />
<input type = "radio"
name = "party" value="Democrat" checked/> Democrat
<br />
<input type = "radio"
name = "party" value="Republican"/> Republican
</p>
<!-- pop-up menu -->
<p>
State: <br />
<select name =
"state">
<option value =
"CA">California</option>
<option value =
"OR">Oregon</option>
<option value =
"NM">New Mexico</option>
</select>
</p>
<!-- text area -->
<p>
Comments: <br />
<textarea name =
"comments" rows = "5" cols = "40" >
Your comments would be appreciated
</textarea>
</p>
<!-- check box -->
<p>
Send me more information:
<input type =
"checkbox" name = "more info" />
</p>
<!-- buttons -->
<p>
<input type = "reset"
value = "Clear Form" />
<input type = "submit"
value = "Send Info" />
</p>
</form>
<html>
<head> <title> Display HTTP Request </title> </head>
<body>
<p> Here's the request you sent to the server: </p>
<%
java.util.Enumeration attNames
= request.getAttributeNames();
java.util.Enumeration paramNames
= request.getParameterNames();
java.util.Enumeration hdrNames
= request.getHeaderNames();
%>
Request =
<%= request.getMethod() %>
<%= request.getServerName() %>:
<%= request.getServerPort() %>
<%= request.getProtocol() %>
<hr />
Request headers: <br/>
<% while
(hdrNames.hasMoreElements()) {
String name = (String)
hdrNames.nextElement();
String value =
request.getHeader(name);
out.println("..." + name +
": " + value + "<br />");
} %>
<hr />
Request parameters: <br/>
<% while
(paramNames.hasMoreElements()) {
String name = (String)
paramNames.nextElement();
String value = request.getParameter(name);
out.println("..." + name +
": " + value + "<br />");
} %>
<hr />
Request attributes: <br/>
<% while
(attNames.hasMoreElements()) {
String name = (String)
attNames.nextElement();
Object value =
request.getAttribute(name);
out.println("..." + name +
": " + value + "<br />");
} %>
<hr />
</body>
</html>
JSP pages may include the following elements:
HTML elements (<form>, <table>, <script>, <applet>, etc.)
<jsp:scriptlet> PARTIAL_STATEMENT
</jsp:scriptlet>
<jsp:expression> EXPRESSION </jsp:expression>
<jsp:declaration> DECLARATION* </jsp:declaration>
<jsp:comment> COMMENT </jsp:comment>
In addition, a jsp page may include standard actions:
<jsp:include>
<jsp:forward>
<jsp:plugin>
<jsp:param>
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>
A jsp page may also include directives:
<jsp:directive> ... </jsp:directive>
also:
<%@ ... %>
<html>
<head><title> Standard Actions </title> </head>
<body>
Here's our company's budget: <br /><br />
<jsp:include page="budget.html" flush = "true" />
</body>
</html>
<table width="100%" border = "1">
<caption align="top"> Acme Inc. Budget for 2003
</caption>
<tr>
<th width="50%"> Item </th>
<th width="50%"> Cost </th>
</tr>
<!-- +++++++++++++++ ROW 1 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Paper Clips
</td>
<td align = "center" width="50%"> $1000 </td>
</td>
</tr>
<!-- +++++++++++++++ ROW 2 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Pencils </td>
<td align = "center" width="50%"> $400 </td>
</td>
</tr>
<!-- +++++++++++++++ ROW 3 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Asprins </td>
<td align = "center" width="50%"> $950 </td>
</td>
</tr>
</table>
public class BudgetBean implements java.io.Serializable {
// properties:
private float paperClips = 1000;
private float asprin = 950;
private float pencils = 450;
// property getters & setters:
public float getPaperClips() { return paperClips; }
public void setPaperClips(float f) { paperClips = f; }
public float getAsprin() { return asprin; }
public void setAsprin(float f) { asprin = f; }
public float getPencils() { return pencils; }
public void setPencils(float f) { pencils = f; }
}
<jsp:useBean id = "budget" scope =
"request" class = "BudgetBean" />
<html>
<head><title> Standard Actions </title> </head>
<body>
<jsp:setProperty name = "budget" property =
"paperClips" value = "100" />
<% budget.setPencils(500); %>
Here's our company's budget: <br /><br />
<jsp:include page="budget.jsp" flush = "true"
/>
</body>
</html>
<jsp:useBean id = "budget" scope =
"request" class = "BudgetBean" />
<table width="100%" border = "1">
<caption align="top"> Acme Inc. Budget for 2003
</caption>
<tr>
<th width="50%"> Item </th>
<th width="50%"> Cost </th>
</tr>
<!-- +++++++++++++++ ROW 1 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Paper Clips
</td>
<td align = "center" width="50%">
$<jsp:getProperty name =
"budget" property = "paperClips" />
</td>
</td>
</tr>
<!-- +++++++++++++++ ROW 2 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Pencils </td>
<td align = "center" width="50%">
$<%= budget.getPencils() %>
</td>
</td>
</tr>
<!-- +++++++++++++++ ROW 3 ++++++++++++++++++++++++ -->
<tr>
<td align = "center" width="50%"> Asprins </td>
<td align = "center" width="50%"> $950 </td>
</td>
</tr>
</table>
Directives are messages to the container that are processed at compilation time. The important directives are:
<%@ page ... %>
<%@ include file = "foo.html" %>
The important page attributes are:
<%@ page import = "..." %>
<%@ page extends = "..." %>
<%@ page errorPage = "..." %>
<%@ page isErroPage = "..." %>
<%@ page isErrorPage = "true" %>
<%@ page import = "java.io.*" %>
<html>
<head> <title> My Error Page </title> </head>
<body>
An error has occurred <br />
<%= exception.getMessage() %> <br />
<% exception.printStackTrace(new PrintWriter(out)); %>
</body>
</html>
<%@ page errorPage = "error.jsp" %>
<%!
void foo1() throws Exception {
foo2(); }
void foo2() throws Exception {
foo3(); }
void foo3() throws Exception { if
(true) throw new Exception("foo3 blew it!"); }
%>
<html>
<head><title> Throw Demo </title> </head>
<body>
I am going to throw an exception now:
<% foo1(); %>
</body>
</html>
See TOMCAT_HOME\work