Use of PageContext objects

Source: Internet
Author: User

(1)

PageContext Object

This object represents the page context, which is used primarily to access shared data between JSPs.

PageContext is an instance of the PageContext class that uses PageContext to access variables in the page, request, session, application range.

GetAttribute (String name): Gets the Name property within the page range.

SetAttribute (String name, value, int scope): If no scope is specified, the property defaults to the page range, such as: Pagecontext.setattribute ("page", "Hello");

Use PageContext to set the property in the REQUEST scope Pagecontext.setattribute ("Request2", "Hello", pagecontext.request_scope);

Use PageContext to set the properties in the SESSION range Pagecontext.setattribute ("Session2", "Hello", pagecontext.session_scope);

Use PageContext to set properties in the application range Pagecontext.setattribute ("app2", "Hello", pagecontext.application_scope);

GetAttribute (String name,int scope): Gets the name attribute within the specified range, where scope can be the following 4 values:

Pagecontext.page_scope: Corresponds to the PAGE range.

Pagecontext.request_scope: Corresponds to the REQUEST range.

Pagecontext.session_scope: Corresponds to the SESSION range.

Pagecontext.application_scope: Corresponds to the application range.

(2)

Instance:

<% @page contenttype= "text/html;charset=gb2312"%>

<body><br>

<%

Use PageContext to set the property, which defaults to the page range

Pagecontext.setattribute ("name", "Jason Test");

Request.setattribute ("name", "Lin Yuan Programming");

Session.setattribute ("name", "Lin yuan computer Programming technical training");

Session.putvalue ("name", "Computer Programming");

Application.setattribute ("name", "training");

%>

Page Set Value: <%=pagecontext.getattribute ("name")%><br>

Request Set Value: <%=pagecontext.getrequest (). getattribute ("name")%><br>

Session Set Value: <%=pagecontext.getsession (). getattribute ("name")%><br>

Application Set Value: <%=pagecontext.getservletcontext (). getattribute ("name")%><br>

Value within range 1: <%=pagecontext.getattribute ("name", 1)%><br>

Value within range 2: <%=pagecontext.getattribute ("name", 2)%><br>

Value within range 3: <%=pagecontext.getattribute ("name", 3)%><br>

Value within range 4: <%=pagecontext.getattribute ("name", 4)%><br>

<!--start with the smallest range page, then the Reques, session, and Application-->

<%pagecontext.removeattribute ("name", 3);%>

PageContext the modified session setting Value: <%=session.getvalue ("name")%><br>

<%pagecontext.setattribute ("name", "Applied Technology Training", 4);%>

PageContext modified Application Set Value: <%=pagecontext.getservletcontext (). getattribute ("name")%><br>

Lookup of the value: <%=pagecontext.findattribute ("name")%><br>

Property name Range: <%=pagecontext.getattributesscope ("name")%><br>

</body>

Page Display results:

Page Set value: Jason Test

Value set by request: Lin Yuan programming

Session setting value: Lin Yuan computer programming technology training

Application Set Value: Training

Value in range 1: Jason test

Value within range 2: Lin Yuan programming

Value within range 3: Lin Yuan computer programming technical training

Values within range 4: Training

PageContext the value of the modified session setting: null

PageContext modified Application Set Value: Application Technical Training

Lookup of values: Jason Test

Property name Range: 1

Use of PageContext objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.