Java Tutorial/JSTL/Page Context

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

Check Page Context Request Method

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Property Access</title>
  </head>
  <body>
    <c:if test="${pageContext.request.method=="POST"}">
    <c:set var="idx" value="name" />
    param.name = 
    <c:out value="${param.name}" />
    <br />
    param[name] = 
    <c:out value="${param[idx]}" />
    <br />
    </c:if>
    <br />
    <form method="post">Please enter your name? 
    <input type="text" name="name" />
    <input type="Submit" />
    <br />
    </form>
  </body>
</html>





Query String

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Query String Example</title>
  </head>
  <body>Your favorite color is: 
  <b>
    <c:out value="${param.color}" />
  </b>
  <br />
  <br />
  Choose your favorite color:
  <br />
  <br />
  



==  Set Class to Page Context ==






   
  <!-- start source code -->
   
    <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>





Set Variable to Page Context

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<%
  synchronized (pageContext) {
    String[] names = {"A", "B", "C", "D"};
    pageContext.setAttribute("names", names, PageContext.PAGE_SCOPE);
  }
%>
<html>
  <head>
    <title>forEach and status</title>
  </head>
  <body>
    <c:forEach var="currentName" items="${pageScope.names}" varStatus="status">
      Family member #<c:out value="${status.count}" /> is
        <c:out value="${currentName}" /> <br />
    </c:forEach>
  </body>
</html>