Java Tutorial/JSTL/Page Context

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

Check Page Context Request Method

   <source lang="java">

<%@ 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}" />
   
param[name] = <c:out value="${param[idx]}" />
</c:if>
<form method="post">Please enter your name? <input type="text" name="name" /> <input type="Submit" />
</form> </body>

</html></source>





Query String

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Query String Example</title>
 </head>
 <body>Your favorite color is: 
 
   <c:out value="${param.color}" />
 
 

Choose your favorite color:


Set Class to Page Context

   <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 Variable to Page Context

   <source lang="java">

<%@ 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}" /> 
</c:forEach> </body>

</html></source>