Set/Get Variable in Application in JSP : Application « JSP « Java Tutorial






index.jsp

<%
  String theSharedObject = "JSP is cool";

  application.setAttribute("message", theSharedObject);
%>

<HTML>
  <HEAD>
    <TITLE>Application Object - Page 1</TITLE>
  </HEAD>

  <BODY>
    This page sets data that can be retrieved by other pages in the application.<P>

    Click <a href="application_page2.jsp">here</a> to see this in action.
  </BODY>
</HTML>

application_page2.jsp

<%
  // getAttribute() returns a java.lang.Object, so need to cast
  String theSharedObject = (String) application.getAttribute("message");
%>

<HTML>
  <HEAD>
    <TITLE>Application Object - Page 2</TITLE>
  </HEAD>

  <BODY>
    This page retrieves the message that was stored in the <i>application</i> object  by another page.
  <P>
    The message is <b><%= theSharedObject %></b>
  </BODY>
</HTML>
  Download:  JSPSetGetVariableInApplication.zip( 939 k)








23.56.Application
23.56.1.Set/Get Variable in Application in JSP