Data is saved using a mechanism called attributes. An attribute is a key/value pair where the key is a string and the value is any object. It is recommended that the key use the reverse domain name convention (e.g., prefixed with com.mycompany) to minimize unexpected collisions when integrating with third party modules.
This example uses attributes to save and retrieve data in each of the four scopes:
<% // Check if attribute has been set Object o = pageContext.getAttribute("com.mycompany.name1", PageContext.PAGE_SCOPE); if (o == null) { // The attribute com.mycompany.name1 may not have a value or may have the value null } // Save data pageContext.setAttribute("com.mycompany.name1", "value0"); // PAGE_SCOPE is the default pageContext.setAttribute("com.mycompany.name1", "value1", PageContext.PAGE_SCOPE); pageContext.setAttribute("com.mycompany.name2", "value2", PageContext.REQUEST_SCOPE); pageContext.setAttribute("com.mycompany.name3", "value3", PageContext.SESSION_SCOPE); pageContext.setAttribute("com.mycompany.name4", "value4", PageContext.APPLICATION_SCOPE); %> <%-- Show the values --%> <%= pageContext.getAttribute("com.mycompany.name1") %> <%-- PAGE_SCOPE --%> <%= pageContext.getAttribute("com.mycompany.name1", PageContext.PAGE_SCOPE) %> <%= pageContext.getAttribute("com.mycompany.name2", PageContext.REQUEST_SCOPE) %> <%= pageContext.getAttribute("com.mycompany.name3", PageContext.SESSION_SCOPE) %> <%= pageContext.getAttribute("com.mycompany.name4", PageContext.APPLICATION_SCOPE) %>
from: http://www.exampledepot.com/egs/javax.servlet.jsp/attr.html
No comments:
Post a Comment