Java/JSP/Page Context
Содержание
Application Object
//File Name: application_page1.jsp
<%
String theSharedObject = "JSP is cool";
application.setAttribute("message", theSharedObject);
%>
<HTML>
<HEAD>
<TITLE>Application Object - Page 1</TITLE>
</HEAD>
<BODY>
This page sets data that can be retrieved by other pages in the application.<P>
Click to see this in action.
</BODY>
</HTML>
//File Name:application_page2.jsp
<%
// getAttribute() returns a java.lang.Object, so need to cast
String theSharedObject = (String) application.getAttribute("message");
%>
<HTML>
<HEAD>
<TITLE>Application Object - Page 2</TITLE>
</HEAD>
<BODY>
This page retrieves the message that was stored in the <i>application</i> object by another page.
<P>
The message is <b><%= theSharedObject %></b>
</BODY>
</HTML>
JSP page scope
<jsp:useBean id="myBookBean" class="com.jexp.Book"
scope="page" />
<html>
<head>
<title>page scope</title>
</head>
<body>
This page declares a page-scoped Book.
</body>
</html>
Page directive attributes
<%@ page autoFlush="true"
buffer="16kb"
contentType="text/html"
errorPage="myErrorPage.jsp"
extends="org.apache.jasper.runtime.HttpJspBase"
import="java.sql.*"
info="This page has a set of page directive attributes"
language="java"
pageEncoding="UTF-8"
session="false"
%>
<HTML>
<HEAD><TITLE>Page directive attributes</TITLE></HEAD>
<BODY>
The JSP page used to generate this content defined values for all of the page directive"s
attributes.
</BODY>
</HTML>
Set JSP PageContext attributes
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<%
synchronized (pageContext) {
Class thisClass = getClass();
// session.setAttribute("thisClass", thisClass);
pageContext.setAttribute("thisClass", thisClass, PageContext.PAGE_SCOPE);
System.out.println("Stored reference");
Class theClass = (Class) pageContext.getAttribute("thisClass", PageContext.PAGE_SCOPE);
System.out.println("The retrieved reference is " + theClass);
}
%>
<html>
<body>
The class that instantiated this JSP is <c:out value="${pageScope.thisClass.name}" />.
</body>
</html>
Set page parameters
<html>
<head>
<title>Set page parameters</title>
</head>
<body>
This page allows you to enter information that is sent as request
parameters to another page. The next page lists them. <P />
<form action="listPageParameters.jsp" method="get">
<table>
<tr><td>Enter an adjective:</td>
<td><input type="text" name="adjective" /></td>
</tr>
<tr><td>Enter a noun:</td>
<td><input type="text" name="noun" /></td>
</tr>
<tr><td>Enter a color:</td>
<td><input type="text" name="color" /></td>
</tr>
</table>
<input type="submit" value="Send parameters" />
</form>
</body>
</html>
//listPageParameters.jsp
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<html>
<head>
<title>List page parameters</title>
</head>
<body>
You entered the following parameters:<br />
<ol>
<%-- "param" is an implicit object. It is a Map that maps a "key"
(the parameter name) to a "value" --%>
<c:forEach var="pageParameter" items="${param}">
<li> <c:out value="${pageParameter.key}" /> = <c:out value="${pageParameter.value}" />
</c:forEach>
</ol>
</body>
</html>