Java/JSTL/Variable Scope

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

JSTL Set Variables Scope

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set in Scope Examples</title>
  </head>
  <body>
    <c:set var="test" value="Page Level Value" scope="page" />
    <c:set var="test" value="Request Level Value"
    scope="request" />
    <c:set var="test" value="Session Level Value"
    scope="session" />
    <c:set var="test" value="Application Level Value"
    scope="application" />
    <table border="1">
<tr>
        <td>
          <b>Default Level</b>
        </td>
        <td>
          <c:out value="${test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Page Level</b>
        </td>
        <td>
          <c:out value="${pageScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Request Level</b>
        </td>
        <td>
          <c:out value="${requestScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Session Level</b>
        </td>
        <td>
          <c:out value="${sessionScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Application Level</b>
        </td>
        <td>
          <c:out value="${applicationScope.test}" />
        </td>
      </tr>
    </table>
  </body>
</html>





Variable scope: page, session and application

   <source lang="java">

//File: index.jsp <%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <c:set var="scopeVarPage" value="Page Value" scope="page" /> <c:set var="scopeVarRequest" value="Request Value" scope="request" /> <c:set var="scopeVarSession" value="Session Value" scope="session" /> <c:set var="scopeVarApplication" value="Application Value" scope="application" /> <html>

 <head>
   <title>Scope Example</title>
 </head>
 <body>

Main File: index.jsp

Scoped Variable Current Value
       Page Scope
(scopeVarPage)
 
       <c:out value="${scopeVarPage}" />
       Request Scope
(scopeVarRequest)
 
       <c:out value="${scopeVarRequest}" />
       Session Scope
(scopeVarSession)
 
       <c:out value="${scopeVarSession}" />
       Application Scope
(applicationVarPage)
 
       <c:out value="${scopeVarApplication}" />
   

<jsp:include page="included.jsp" />