SlideShare uma empresa Scribd logo
1 de 4
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) %>

Mais conteúdo relacionado

Mais procurados (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
 

Destaque

3.jsp tutorial
3.jsp tutorial3.jsp tutorial
3.jsp tutorialshiva404
 
Session 5 Tp5
Session 5 Tp5Session 5 Tp5
Session 5 Tp5phanleson
 
ASP, ASP.NET, JSP, COM/DCOM
ASP, ASP.NET, JSP, COM/DCOMASP, ASP.NET, JSP, COM/DCOM
ASP, ASP.NET, JSP, COM/DCOMAashish Jain
 
Enterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEnterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEmprovise
 
Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Peter R. Egli
 
Entity beans in java
Entity beans in javaEntity beans in java
Entity beans in javaAcp Jamod
 
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 2mehwish88
 
Achieving competitive advantage with information systems
Achieving competitive advantage with information systemsAchieving competitive advantage with information systems
Achieving competitive advantage with information systemsProf. Othman Alsalloum
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJBPeter R. Egli
 

Destaque (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
 

Semelhante a 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
 
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...WebStackAcademy
 
ServletConfig & ServletContext
ServletConfig & ServletContextServletConfig & ServletContext
ServletConfig & ServletContextASHUTOSH TRIVEDI
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSam Brannen
 
Jsp config implicit object
Jsp config implicit objectJsp config implicit object
Jsp config implicit objectchauhankapil
 
Remote code-with-expression-language-injection
Remote code-with-expression-language-injectionRemote code-with-expression-language-injection
Remote code-with-expression-language-injectionMickey Jack
 
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 paperVinay Kumar
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and ODataAnil Allewar
 
Boston Computing Review - Java Server Pages
Boston Computing Review - Java Server PagesBoston Computing Review - Java Server Pages
Boston Computing Review - Java Server PagesJohn Brunswick
 
Apache Struts 2 Advance
Apache Struts 2 AdvanceApache Struts 2 Advance
Apache Struts 2 AdvanceEmprovise
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESYoga Raja
 
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)James Titcumb
 

Semelhante a 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
 

Mais de vikram singh

Mais de 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
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 

Último (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 

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) %>