SlideShare a Scribd company logo
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.
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
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 --%>
<%= pageContext.getAttribute(quot;com.mycompany.name1quot;,
PageContext.PAGE_SCOPE) %>
    <%= pageContext.getAttribute(quot;com.mycompany.name2quot;,
PageContext.REQUEST_SCOPE) %>
    <%= pageContext.getAttribute(quot;com.mycompany.name3quot;,
PageContext.SESSION_SCOPE) %>
    <%= pageContext.getAttribute(quot;com.mycompany.name4quot;,
PageContext.APPLICATION_SCOPE) %>

More Related Content

What's hot

What's hot (20)

Jsp
JspJsp
Jsp
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Jsp
JspJsp
Jsp
 
Jsp tutorial (1)
Jsp tutorial (1)Jsp tutorial (1)
Jsp tutorial (1)
 
Jsp lecture
Jsp lectureJsp lecture
Jsp lecture
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
Jsp element
Jsp elementJsp element
Jsp element
 
JSP
JSPJSP
JSP
 
Jsp Introduction Tutorial
Jsp Introduction TutorialJsp Introduction Tutorial
Jsp Introduction Tutorial
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
Jsp (java server page)
Jsp (java server page)Jsp (java server page)
Jsp (java server page)
 
Jsp slides
Jsp slidesJsp slides
Jsp slides
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Jsp(java server pages)
Jsp(java server pages)Jsp(java server pages)
Jsp(java server pages)
 
Deploying java beans in jsp
Deploying java beans in jspDeploying java beans in jsp
Deploying java beans in jsp
 
Jsp Slides
Jsp SlidesJsp Slides
Jsp Slides
 

Viewers also liked

3.jsp tutorial
3.jsp tutorial3.jsp tutorial
3.jsp tutorial
shiva404
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5
phanleson
 
Role of technology in business communication 2
Role of technology in business communication 2Role of technology in business communication 2
Role of technology in business communication 2
mehwish88
 
Achieving competitive advantage with information systems
Achieving competitive advantage with information systemsAchieving competitive advantage with information systems
Achieving competitive advantage with information systems
Prof. Othman Alsalloum
 

Viewers also liked (20)

jsp tutorial
jsp tutorialjsp tutorial
jsp tutorial
 
3.jsp tutorial
3.jsp tutorial3.jsp tutorial
3.jsp tutorial
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5
 
Ejb3 Presentation
Ejb3 PresentationEjb3 Presentation
Ejb3 Presentation
 
ASP, ASP.NET, JSP, COM/DCOM
ASP, ASP.NET, JSP, COM/DCOMASP, ASP.NET, JSP, COM/DCOM
ASP, ASP.NET, JSP, COM/DCOM
 
Java Servlets & JSP
Java Servlets & JSPJava Servlets & JSP
Java Servlets & JSP
 
Unified Expression Language
Unified Expression LanguageUnified Expression Language
Unified Expression Language
 
Enterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEnterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business Logic
 
INFORMATION TECHNOLOGY FOR BUSINESS
INFORMATION TECHNOLOGY FOR BUSINESSINFORMATION TECHNOLOGY FOR BUSINESS
INFORMATION TECHNOLOGY FOR BUSINESS
 
Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)
 
Entity beans in java
Entity beans in javaEntity beans in java
Entity beans in java
 
Java bean
Java beanJava bean
Java bean
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
 
EJB3 Basics
EJB3 BasicsEJB3 Basics
EJB3 Basics
 
Jsp
JspJsp
Jsp
 
Strategic use of information systems
Strategic use of information systemsStrategic use of information systems
Strategic use of information systems
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Role of technology in business communication 2
Role of technology in business communication 2Role of technology in business communication 2
Role of technology in business communication 2
 
Achieving competitive advantage with information systems
Achieving competitive advantage with information systemsAchieving competitive advantage with information systems
Achieving competitive advantage with information systems
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
 

Similar to JSP Scope variable And Data Sharing

The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
SPTechCon
 
Remote code-with-expression-language-injection
Remote code-with-expression-language-injectionRemote code-with-expression-language-injection
Remote code-with-expression-language-injection
Mickey Jack
 

Similar to JSP Scope variable And Data Sharing (20)

The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
 
Jsp
JspJsp
Jsp
 
Jsp session 3
Jsp   session 3Jsp   session 3
Jsp session 3
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 6...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 6... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 6...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 6...
 
ServletConfig & ServletContext
ServletConfig & ServletContextServletConfig & ServletContext
ServletConfig & ServletContext
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Jsp config implicit object
Jsp config implicit objectJsp config implicit object
Jsp config implicit object
 
Ajax Lecture Notes
Ajax Lecture NotesAjax Lecture Notes
Ajax Lecture Notes
 
Remote code-with-expression-language-injection
Remote code-with-expression-language-injectionRemote code-with-expression-language-injection
Remote code-with-expression-language-injection
 
Tuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paperTuning and optimizing webcenter spaces application white paper
Tuning and optimizing webcenter spaces application white paper
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and OData
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
I Feel Pretty
I Feel PrettyI Feel Pretty
I Feel Pretty
 
Boston Computing Review - Java Server Pages
Boston Computing Review - Java Server PagesBoston Computing Review - Java Server Pages
Boston Computing Review - Java Server Pages
 
Apache Struts 2 Advance
Apache Struts 2 AdvanceApache Struts 2 Advance
Apache Struts 2 Advance
 
Jsp advance part i
Jsp advance part iJsp advance part i
Jsp advance part i
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
J2EE jsp_03
J2EE jsp_03J2EE jsp_03
J2EE jsp_03
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
User Interface
User InterfaceUser Interface
User Interface
 

More from vikram singh

More from vikram singh (20)

Agile
AgileAgile
Agile
 
Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2Enterprise java beans(ejb) Update 2
Enterprise java beans(ejb) Update 2
 
Web tech importants
Web tech importantsWeb tech importants
Web tech importants
 
Enterprise Java Beans( E)
Enterprise  Java  Beans( E)Enterprise  Java  Beans( E)
Enterprise Java Beans( E)
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2
 
Enterprise java beans(ejb)
Enterprise java beans(ejb)Enterprise java beans(ejb)
Enterprise java beans(ejb)
 
2 4 Tree
2 4 Tree2 4 Tree
2 4 Tree
 
23 Tree Best Part
23 Tree   Best Part23 Tree   Best Part
23 Tree Best Part
 
Bean Intro
Bean IntroBean Intro
Bean Intro
 
jdbc
jdbcjdbc
jdbc
 
Sax Dom Tutorial
Sax Dom TutorialSax Dom Tutorial
Sax Dom Tutorial
 
Xml
XmlXml
Xml
 
Dtd
DtdDtd
Dtd
 
Xml Schema
Xml SchemaXml Schema
Xml Schema
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
 
Servlet Part 2
Servlet Part 2Servlet Part 2
Servlet Part 2
 
Tutorial Solution
Tutorial SolutionTutorial Solution
Tutorial Solution
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorial
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 
Sample Report Format
Sample Report FormatSample Report Format
Sample Report Format
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 

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 --%>
  • 4. <%= pageContext.getAttribute(quot;com.mycompany.name1quot;, PageContext.PAGE_SCOPE) %> <%= pageContext.getAttribute(quot;com.mycompany.name2quot;, PageContext.REQUEST_SCOPE) %> <%= pageContext.getAttribute(quot;com.mycompany.name3quot;, PageContext.SESSION_SCOPE) %> <%= pageContext.getAttribute(quot;com.mycompany.name4quot;, PageContext.APPLICATION_SCOPE) %>