File Name: implicitobjectscope. jsp
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix = "C" %>
<JSP: usebean id = "simplebean" Scope = "request" class = "jsptag. simplebean">
</Jsp: usebean>
<HTML>
<Head>
<Title>
Implicitobjectscope
</Title>
</Head>
<Body bgcolor = "# ffffff">
<H3>
Implicitobjectscope. jsp
</H3>
<P> <strong>
First, set the scope to four variables of application/session/Request/page.
</Strong> </P>
<C: Set Var = "applicationvar" value = "applicationvarvalue" Scope = "application"/>
<C: Set Var = "sessionvar" value = "sessionvarvalue" Scope = "session"/>
<C: Set Var = "requestvar" value = "requestvarvalue" Scope = "request"/>
<C: Set Var = "pagevar" value = "pagevarvalue" Scope = "page"/>
<% // Equivalent to the preceding one
/*
Session. setattribute ("sessionvar", "sessionvarvalue ");
Application. setattribute ("applicationvar", "applicationvarvalue ");
Request. setattribute ("requestvar", "requestvarvalue ");
Pagecontext. setattribute ("pagevar", "pagevarvalue ");
*/%>
Session variable value:
<C: Out value = "$ {sessionscope. sessionvar}" default = "There is not session variable"/>
<Br/>
Application variable value:
<C: Out value = "$ {applicationscope. applicationvar}">
There is not application variable"
</C: Out>
<Br/>
Request variable value:
<C: Out value = "$ {requestscope. requestvar}">
There is not request variable
</C: Out>
<Br/>
Page variable value:
<C: Out value = "$ {pagination. pagevar}">
There is not page variable
</C: Out>
<P>
<Form action = "implicitobjectscope. jsp" method = "Post">
<Input type = "hidden" name = "requestname" value = "this is the value of requestname"/>
<Input type = "hidden" name = "isforward" value = "Y"/>
<Input type = "Submit" value = "Click here to submit to this page and forward the request to the implicitobjectscope1 page"/>
</Form>
<Form action = "implicitobjectscope. jsp" method = "Post">
<Input type = "hidden" name = "requestname" value = "this is the value of requestname"/>
<Input type = "hidden" name = "isforward" value = "Y"/>
<Input type = "hidden" name = "isforwaragain" value = "Y"/>
<Input type = "Submit" value = "Click here to submit to this page, forward the request to the implicitobjectscope1 page, and forward the request to the destofforward page again"/>
</Form>
<Form action = "implicitobjectscope1.jsp" method = "Post">
<Input type = "hidden" name = "requestname" value = "this is the value of requestname"/>
<Input type = "Submit" value = "Click here to submit directly to the implicitobjectscope1 page"/>
</Form>
<Form action = "implicitobjectscope1.jsp" method = "Post">
<Input type = "hidden" name = "requestname" value = "this is the value of requestname"/>
<Input type = "hidden" name = "isforwaragain" value = "Y"/>
<Input type = "Submit" value = "Click here to directly submit the request to the implicitobjectscope1 page, and then forward the request to the destofforward page"/>
</Form>
</P>
<C: If test = "$ {not empty Param. isforward}">
<JSP: Forward page = "implicitobjectscope1.jsp"/>
</C: If>
<P>
The following is an example of using the foreach tag to accumulate data (El is used with JavaBean ).
</P>
<C: Set Value = "0" Var = "count">
</C: Set>
<Table border = "1">
<Tr>
<TD> index </TD> <TD> Number </TD> <TD> sum </TD>
</Tr>
<C: foreach items = "$ {simplebean. numberlist}" Var = "Number" varstatus = "status">
<Tr>
<TD> <C: Out value = "$ {status. Index}"/> </TD>
<TD>
<C: Out value = "$ {number}"/>
<C: Set Var = "count2" value = "$ {count}"/>
<C: Set Var = "count" value = "$ {count2 + number}"/>
</TD>
<TD> count: <C: Out value = "$ {count}"/> </TD>
</Tr>
</C: foreach>
</Body>
</Html>
File Name: implicitobjectscope1.jsp
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix = "C" %>
<JSP: usebean id = "simplebean" Scope = "request" class = "jsptag. simplebean">
</Jsp: usebean>
<JSP: setproperty name = "simplebean" property = "*"/>
<C: If test = "$ {not empty Param. isforwaragain}">
<JSP: Forward page = "destofforward. jsp"/>
</C: If>
<HTML>
<Head>
<Title>
Implicitobjectscope1
</Title>
</Head>
<Body bgcolor = "# ffffff">
<H3>
Implicitobjectscope1.jsp
</H3>
<P>
Make sure to compare variables of different scopes and check whether their values have changed.
</P>
<P>
Session variable value:
<C: Out value = "$ {sessionscope. sessionvar}" default = "There is not session variable"/>
<Br/>
Application variable value:
<C: Out value = "$ {applicationscope. applicationvar}">
There is not application variable"
</C: Out>
<Br/>
Request variable value:
<C: Out value = "$ {requestscope. requestvar}">
There is not request variable
</C: Out>
<Br/>
Page variable value:
<C: Out value = "$ {pagination. pagevar}">
There is not page variable
</C: Out>
</P>
<P>
The following is an example of combining El with JavaBean:
<Br/>
Output Value of reqeustname:
<C: Out value = "$ {simplebean. requestname}">
The simplebean. requestname is not existed.
</C: Out>
</P>
</Table>
</Body>
</Html>
File Name: destofforward. jsp
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix = "C" %>
<JSP: usebean id = "simplebean" Scope = "page" class = "jsptag. simplebean"/>
<% -- Differences between changing scope to request -- %>
<% -- JSP: usebean id = "simplebean" Scope = "page" class = "jsptag. simplebean"/-- %>
<HTML>
<Head>
<Title>
Destofforward
</Title>
</Head>
<Body bgcolor = "# ffffff">
<H3>
Destofforward. jsp
</H3>
<P>
Compare the output changes here.
<Br/>
Request variable:
<Strong>
<C: Out value = "$ {requestscope. requestvar}">
There is not request variable
</C: Out>
</Strong>
</P>
<Br/>
Output Value of reqeustname:
<Strong>
<C: Out value = "$ {simplebean. requestname}">
The simplebean. requestname is not existed.
</C: Out>
</Strong>
<Br/>
<P>
Note: here, the scope of simplean (simplebean) is page, which is different from the previous page for request, so the output is non-existent.
</P>
</Body>
</Html>
File Name: simplebean. Java
Package jsptag;
/**
* <P> title: </P>
* <P> Description: A simple Java Bean </P>
* <P> copyright: Copyright (c) 2004 </P>
* <P> company: </P>
* @ Author zqy
* @ Version 1.0
*/
Import java. util. arraylist;
Public class simplebean {
Private string requestname = NULL;
Private arraylist numberlist = new arraylist ();
Public simplebean (){
For (INT I = 1; I <= 10; I ++)
This. numberlist. Add (string. valueof (I ));
}
Public void setrequestname (string requestname ){
This. requestname = requestname;
}
Public String getrequestname (){
Return this. requestname;
}
Public void setnumberlist (arraylist numberlist)
{
This. numberlist = numberlist;
}
Public arraylist getnumberlist ()
{
Return this. numberlist;
}
}
Supplement:
The following section describes how to differentiate page scope and request scope:
"Objects are created within a JSP page instance that is responding to a request object. There are several scopes:
Page-objects with page scope are accessible only within the page where they are created. all references to such an object shall be released after the response is sent back to the client from the JSP page or the request is forwarded somewhere else. references to objects with page scope are stored in the pagecontext object.
Request-objects with request scope are accessible from pages processing the same request where they were created. references to the object shall be re-leased after the request is processed. in particle, if the request is forwarded to a resource in the same runtime, the object is still reachable. references to objects with request scope are stored in the request object."