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>