Kamis, 08 Januari 2015

Scope


Tutorial membuat program scope pada netBeans

1. Buka netBeans

 2. Pilih new project


3. Choose Project (java web - Web application) lalu next


4. Klik finish


5. Lalu tuliskan kode berikut

Nama 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</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>
        
      </tr>

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

        (scopeVarRequest)</td>

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

        (scopeVarSession)</td>
        
      </tr>

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

        (applicationVarPage)</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>

6. Lalu buatkan lagi file bernama 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>

      </tr>

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

        (scopeVarRequest)</td>

      </tr>

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

        (scopeVarSession)</td>

      </tr>

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

        (applicationVarPage)</td>

      </tr>
    </table>
  </body>
</html>

7. Dan Buat juga 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>
  </tr>

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

    (scopeVarRequest)</td>

  </tr>

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

    (scopeVarSession)</td>
  </tr>

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

    (applicationVarPage)</td>
 
  </tr>
</table>

Maka akan diperoleh output seperti ini:




Sumber: http://www.dineshonjava.com/2013/09/jsp-scopes-example.html#.VK1rQvmSxUk