Java/JSP/Page Context

Материал из Java эксперт
Перейти к: навигация, поиск

Application Object

   <source lang="java">

//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.

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 application object by another page. <P> The message is <%= theSharedObject %> </BODY> </HTML> </source>

JSP page scope

   <source lang="java">

<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>

      </source>
   
  
 
  



Page directive attributes

   <source lang="java">

<%@ 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>


      </source>
   
  
 
  



Set JSP PageContext attributes

   <source lang="java">

<%@ 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>


      </source>
   
  
 
  



Set page parameters

   <source lang="java">

<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">
Enter an adjective: <input type="text" name="adjective" />
Enter a noun: <input type="text" name="noun" />
Enter a color: <input type="text" name="color" />
     <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:
    <%-- "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}">
  1. <c:out value="${pageParameter.key}" /> = <c:out value="${pageParameter.value}" /> </c:forEach>
 </body>

</html>


      </source>