Four types of object scopes in JSPs

Source: Internet
Author: User
Tags ranges

Page: The current pages, that is, as long as the other pages are not valid, can be approximated to understand the Java this object

Request: A session, a simple understanding is the scope of a request is valid, for example, if the forward way to jump, the forward target page can still get the property value in request, for example, if the page jump by redirect way, The value in request disappears because redirect is equivalent to a re-issued request

Session: Browser process, as long as the current page is not closed (not forced by the browser to clear), regardless of how the jump is valid (also can be said to exist in a session cycle)

Application: Server, data is valid as long as the server is not restarted (not enforced by the program)

pagescope.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<%@ page import="java.util.*" %>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<body>

<%

Set data for two page ranges Key->value

Pagecontext.setattribute ("name", "Page_barry");

Pagecontext.setattribute ("Age", 21);

%>

<%

Take value

String Name= (String) pagecontext.getattribute ("name");

int age= (Integer) Pagecontext.getattribute ("Age");

%>

<font> Name: <%=name%></font>

<font> Age: <%=age%></font>

</body>

pagecontext01.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<%@ page import="java.util.*" %>

<%@ page errorpage="error.jsp" %>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<%

Pagecontext.setattribute ("NAME0", "PageInfo");

Request.setattribute ("Name1", "Requestinfo");

Session.setattribute ("Name2", "Sessioninfo");

Application.setattribute ("Name3", "ApplicationInfo");

Out.println ("Use pagecontext<br/>");

Out.println ("attribute value in page:" +pagecontext.getattribute ("NAME0") + "<br/>");

Out.println ("attribute value in Request:" +pagecontext.getrequest (). getattribute ("name1") + "<br/>");

Out.println ("attribute value in session:" +pagecontext.getsession (). getattribute ("name2") + "<br/>");

Out.println ("attribute value in Application:" +pagecontext.getservletcontext (). getattribute ("Name3") + "<br/>");

%>

</body>

pagescope_2.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<%@ page import="java.util.*" %>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<body>

<%

Set data for two page ranges Key->value

Pagecontext.setattribute ("name", "Page_barry");

Pagecontext.setattribute ("Age", 21);

%>

<jsp:forward page="pagetarget_2.jsp"></jsp:forward>

</body>

pagetarget_2.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<%@ page import="java.util.*" %>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<body>

<%

Take value

String Name= (String) pagecontext.getattribute ("name");

int age= (Integer) Pagecontext.getattribute ("Age");

%>

<font> Name: <%=name%></font>

<font> Age: <%=age%></font>

</body>

requestscope.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<%

Set data for two request ranges Key->value

Request.setattribute ("name", "Request_barry");

Request.setattribute ("Age", 12);

%>

<jsp:forward page="requesttarget.jsp"></jsp:forward>

</body>

requesttarget.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<%@ page import="java.util.*" %>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<%

Take value

String Name= (String) request.getattribute ("name");

int age= (Integer) Request.getattribute ("Age");

Get header information

Enumeration Enu=request.getheadernames ();

while (Enu.hasmoreelements ()) {

String Headername= (String) enu.nextelement ();

String Headervalue=request.getheader (headername);

%>

<%

}

%>

<font> name <%=name%></font><br>

<font> Age <%=age%></font>

</body>

requestscope_2.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<%

Set data for two request ranges Key->value

Request.setattribute ("name", "Request_barry");

Request.setattribute ("Age", 12);

%>

<%response.sendredirect ("requesttarget_2"); %>

</body>

requesttarget_2.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<%@ page import="java.util.*" %>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<%

Take value

String Name= (String) request.getattribute ("name");

int age= (Integer) Request.getattribute ("Age");

Get header information

Enumeration Enu=request.getheadernames ();

while (Enu.hasmoreelements ()) {

String Headername= (String) enu.nextelement ();

String Headervalue=request.getheader (headername);

%>

<%

}

%>

<font> name <%=name%></font><br>

<font> Age <%=age%></font>

</body>

sessionscope.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<%

Set the data for the two session range Key->value

Session.setattribute ("name", "Session_barry");

Session.setattribute ("Age", 12);

%>

The

</body>

sessiontarget.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<%

String Name= (String) session.getattribute ("name");

Integer age= (integer) Session.getattribute ("Age");

%>

<font> name <%=name%></font><br>

<font> Age <%=age%></font>

</body>

applicationscope.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<%

Set data for two application ranges Key->value

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

Application.setattribute ("Age", 12);

%>

The

</body>

applicationtarget.jsp

<%@ page language="java" contenttype="text/html; Charset=utf-8 "

pageencoding="Utf-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

<%

Take value

String Name= (String) application.getattribute ("name");

int age= (Integer) Application.getattribute ("Age");

%>

<font> name <%=name%></font><br>

<font> Age <%=age%></font>

</body>

Four types of object scopes in JSPs

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.