Time Series Foundation Models - current state and future directions
JSP Scope variable And Data Sharing
1. Scope of JSP objects:
The availability of a JSP object for use from a particular place of the application is
defined as the scope of that JSP object. Every object created in a JSP page will have a
scope. Object scope in JSP is segregated into four parts and they are page, request,
session and application.
• page
‘page’ scope means, the JSP object can be accessed only from within the same
page where it was created. The default scope for JSP objects created using
<jsp:useBean> tag is page. JSP implicit objects out, exception, response,
pageContext, config and page have ‘page’ scope.
• request
A JSP object created using the ‘request’ scope can be accessed from any pages
that serves that request. More than one page can serve a single request. The JSP
object will be bound to the request object. Implicit object request has the ‘request’
scope.
• session
’session’ scope means, the JSP object is accessible from pages that belong to the
same session from where it was created. The JSP object that is created using the
session scope is bound to the session object. Implicit object session has the
’session’ scope.
• application
A JSP object created using the ‘application’ scope can be accessed from any
pages across the application. The JSP object is bound to the application object.
Implicit object application has the ‘application’ scope.
Saving Data in a Servlet
There are three places a servlet can save data for its processing - in the request, in the
session (if present), and in the servlet context (or application, which is shared by all
servlets and JSP pages in the context).
Data saved in the request is destroyed when the request is completed.
Data saved in the session is destroyed when the session is destroyed.
Data saved in the servlet context is destroyed when the servlet context is destroyed. Or
application is terminated.
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.
Note: The three locations are identical to the three scopes -- request, session, and
application - - on a JSP page. So, if a servlet saves data in the request, the data will be
available on a JSP page in the request scope.
A JSP page-scoped value is simply implemented by a local variable in a servlet.
2. void setAttribute(String name,Object value);
Object getAttribute(String name);
This example saves and retrieves data in each of the three places:
// Save and get a request-scoped value
req.setAttribute(quot;com.mycompany.req-paramquot;, quot;req-valuequot;);
Object value = req.getAttribute(quot;com.mycompany.req-paramquot;);
// Save and get a session-scoped value
HttpSession session = req.getSession(false);
if (session != null) {
session.setAttribute(quot;com.mycompany.session-paramquot;, quot;session-
valuequot;);
value = session.getAttribute(quot;com.mycompany.session-paramquot;);
}
// Save and get an application-scoped value
getServletContext().setAttribute(quot;com.mycompany.app-paramquot;, quot;app-
valuequot;);
value = getServletContext().getAttribute(quot;com.mycompany.app-paramquot;);
The following example retrieves all attributes in a scope:
// Get all request-scoped attributes
java.util.Enumeration enum = req.getAttributeNames();
for (; enum.hasMoreElements(); ) {
// Get the name of the attribute
String name = (String)enum.nextElement();
// Get the value of the attribute
Object value = req.getAttribute(name);
}
// Get all session-scoped attributes
HttpSession session = req.getSession(false);
if (session != null) {
enum = session.getAttributeNames();
for (; enum.hasMoreElements(); ) {
// Get the name of the attribute
String name = (String)enum.nextElement();
// Get the value of the attribute
Object value = session.getAttribute(name);
}
}
// Get all application-scoped attributes
enum = getServletContext().getAttributeNames();
for (; enum.hasMoreElements(); ) {
// Get the name of the attribute
String name = (String)enum.nextElement();
// Get the value of the attribute
3. Object value = getServletContext().getAttribute(name);
}
Saving Data in a JSP Page
When a JSP page needs to save data for its processing, it must specify a location, called
the scope. There are four scopes available - page, request, session, and application. Page -
scoped data is accessible only within the JSP page and is destroyed when the page has
finished generating its output for the request. Request-scoped data is associated with the
request and destroyed when the request is completed. Session-scoped data is associated
with a session and destroyed when the session is destroyed. Application-scoped data is
associated with the web application and destroyed when the web application is destroyed.
Application-scoped data is not accessible to other web applications.
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(quot;com.mycompany.name1quot;,
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(quot;com.mycompany.name1quot;, quot;value0quot;); //
PAGE_SCOPE is the default
pageContext.setAttribute(quot;com.mycompany.name1quot;, quot;value1quot;,
PageContext.PAGE_SCOPE);
pageContext.setAttribute(quot;com.mycompany.name2quot;, quot;value2quot;,
PageContext.REQUEST_SCOPE);
pageContext.setAttribute(quot;com.mycompany.name3quot;, quot;value3quot;,
PageContext.SESSION_SCOPE);
pageContext.setAttribute(quot;com.mycompany.name4quot;, quot;value4quot;,
PageContext.APPLICATION_SCOPE);
%>
<%-- Show the values --%>
<%= pageContext.getAttribute(quot;com.mycompany.name1quot;) %> <%--
PAGE_SCOPE --%>