SlideShare una empresa de 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) %>

Más contenido relacionado

La actualidad más candente (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
 

Destacado

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
 

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

Similar 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
 

Más de vikram singh

Más 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

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 

Último (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

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