JSP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
JSP Tutorial
Introduction Introduction
Installing JDK-Windows Installing JDK-Windows
Installing JDK-Linux Installing JDK-Linux
Installing Tomcat Installing Tomcat
Simple Program in JSP Simple Program in JSP
Pre-defined Variables Pre-defined Variables
Syntax of Tags Syntax of Tags
JSP Expression Language JSP Expression Language
JSP EL Operators JSP EL Operators
EL Implicit Variables EL Implicit Variables
Scopes Scopes
Using Arrays Using Arrays
Methods Methods
JSP Elements JSP Elements
Including Beans Including Beans
JSTL Tags JSTL Tags
Custom Tags Custom Tags
Session Session in JSP
Handling Cookies Handling Cookies
JSP Application Structure JSP Application Structure
Configuring web.xml Configuring web.xml
Tomcat Configuration Tomcat Configuration
JSP Examples JSP Examples
Forums Ask Your Doubts
Feedback Feedback
 






outsourced web hosting support

JSP Object Scope


Tutorials Jsp

Topic

Defining Scope for a JSP Object?




Explanation

The availability of an JSP object to be used is usually defined by the scope of that object.

Page Scope:

Using this the JSP object can be used within the page whee it was created.

Request Scope:

Using this the JSP object can used from anywhere the request is being served.

Session scope:

Using this the JSP object can be used from pages belonging to the same session.

Application scope:

Using this the JSP object can used from pages across the application.

Example:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <c:set var="Pag" value="Page Value" scope="page" />
    <c:set var="Req" value="Request Value" scope="request" />
    <c:set var="Ses" value="Session Value" scope="session" />
    <c:set var="App" value="Application Value" scope="application" />
    <html>
    <body>
    <b>Page Scope</b>       ::<c:out value="${Pag}" /><br>
    <b>Request Scope</b>    ::<c:out value="${Req}" /><br>
    <b>Session Scope</b>    ::<c:out value="${Ses}" /><br>
    <b>Application Scope</b>::<c:out value="${App}" /><br>
    <a href="scope2.jsp">Go nextpage for Session,Application Scope</a>
    </body>
    </html>
Result
    Page Scope ::Page Value
    Request Scope ::Request Value
    Session Scope ::Session Value
    Application Scope::Application Value
    Goto to next page to know the Session,Application Scope
Example:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
    <body>
    <b>Page Scope</b>       ::<c:out value="${Pag}" /><br>
    <b>Request Scope</b>    ::<c:out value="${Req}" /><br>
    <b>Session Scope</b>    ::<c:out value="${Ses}" /><br>
    <b>Application Scope</b>::<c:out value="${App}" /><br>
    </body>
    </html>
Result
    Page Scope        ::
    Request Scope     ::
    Session Scope     ::Session Value
    Application Scope ::Application Value  

In the above example "scope1.jsp" variables are set in all the four scopes are displayed. When clicked on a link and moved to the next page "scope2.jsp" to display all the variables only the session and application scope variables are displayed as there is no page, request scope for the second page.






A Note
JSP(Java Server Pages) is one of the most used Server Side Programming Language in the world. Simple introduction, basic program codes with examples. Begin coding your own JSP scripts with this online tutorial. Hope you enjoy this tutorial. Do send your feedback or suggestions on this JavaServer Pages tutorial. This is a copyright content.


Other Links

web hosting