Variable scope: page, session and application : Variable Scope « JSTL « Java






Variable scope: page, session and application

//File: index.jsp
<%@ taglib uri="http://java.sun.com/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>
    <h3>Main File: index.jsp</h3>

    <table border="1">
      <tr>
        <th>Scoped Variable</th>

        <th>Current Value</th>
      </tr>

      <tr>
        <td>
        <b>Page Scope</b>

        (scopeVarPage)</td>

        <td>&#160; 
        <c:out value="${scopeVarPage}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Request Scope</b>

        (scopeVarRequest)</td>

        <td>&#160; 
        <c:out value="${scopeVarRequest}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Session Scope</b>

        (scopeVarSession)</td>

        <td>&#160; 
        <c:out value="${scopeVarSession}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Application Scope</b>

        (applicationVarPage)</td>

        <td>&#160; 
        <c:out value="${scopeVarApplication}" />
        </td>
      </tr>
    </table>

    <br />

    <br />

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

    <br />

    <br />

    <a href="linked.jsp">[Click Here to View: linked.jsp]</a>
  </body>
</html>

//////////////////////////////////////////////////////////////////////////////////////////
//File: linked.jsp
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Scope Example</title>
  </head>

  <body>
    <h3>Linked File: linked.jsp</h3>

    <table border="1">
      <tr>
        <th>Scoped Variable</th>

        <th>Current Value</th>
      </tr>

      <tr>
        <td>
        <b>Page Scope</b>

        (scopeVarPage)</td>

        <td>&#160; 
        <c:out value="${scopeVarPage}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Request Scope</b>

        (scopeVarRequest)</td>

        <td>&#160; 
        <c:out value="${scopeVarRequest}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Session Scope</b>

        (scopeVarSession)</td>

        <td>&#160; 
        <c:out value="${scopeVarSession}" />
        </td>
      </tr>

      <tr>
        <td>
        <b>Application Scope</b>

        (applicationVarPage)</td>

        <td>&#160; 
        <c:out value="${scopeVarApplication}" />
        </td>
      </tr>
    </table>
  </body>
</html>

//////////////////////////////////////////////////////////////////////////////////////////
//File: included.jsp
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<table border="1">
  <tr>
    <th>Scoped Variable</th>

    <th>Current Value</th>
  </tr>

  <tr>
    <td>
    <b>Page Scope</b>

    (scopeVarPage)</td>

    <td>&#160; 
    <c:out value="${scopeVarPage}" />
    </td>
  </tr>

  <tr>
    <td>
    <b>Request Scope</b>

    (scopeVarRequest)</td>

    <td>&#160; 
    <c:out value="${scopeVarRequest}" />
    </td>
  </tr>

  <tr>
    <td>
    <b>Session Scope</b>

    (scopeVarSession)</td>

    <td>&#160; 
    <c:out value="${scopeVarSession}" />
    </td>
  </tr>

  <tr>
    <td>
    <b>Application Scope</b>

    (applicationVarPage)</td>

    <td>&#160; 
    <c:out value="${scopeVarApplication}" />
    </td>
  </tr>
</table>
           
       








JSTL-DataScope-Page-Session-Application.zip( 855 k)

Related examples in the same category

1.JSTL Set Variables Scope