javabean四个作用域范围的详解
一 说明
使用usebeans的scope属性可以用来指定javabean的作用范围。
二 四个作用范围
三 代码
1、login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<%@ page language= "java" import = "java.util.*" contenttype= "text/html; charset=utf-8" %>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+ "://" +request.getservername()+ ":" +request.getserverport()+path+ "/" ;
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" >
<html>
<head>
<base href= "<%=basepath%>" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
<title>my jsp 'login.jsp' starting page</title>
<meta http-equiv= "pragma" content= "no-cache" >
<meta http-equiv= "cache-control" content= "no-cache" >
<meta http-equiv= "expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "this is my page" >
<!--
<link rel= "stylesheet" type= "text/css" href= "styles.css" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
-->
</head>
<body>
<h1>系统登录</h1>
<hr>
<form name= "loginform" action= "dologin.jsp?mypass=999999" method= "post" >
<table>
<tr>
<td>用户名:</td>
<td><input type= "text" name= "username" value= "" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type= "password" name= "password" value= "" /></td>
</tr>
<tr>
<td colspan= "2" align= "center" ><input type= "submit" value= "登录" /></td>
</tr>
</table>
</form>
</body>
</html>
|
2、dologin.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
<%@ page language= "java" import = "java.util.*" contenttype= "text/html; charset=utf-8" %>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+ "://" +request.getservername()+ ":" +request.getserverport()+path+ "/" ;
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" >
<html>
<head>
<base href= "<%=basepath%>" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
<title>my jsp 'dologin.jsp' starting page</title>
<meta http-equiv= "pragma" content= "no-cache" >
<meta http-equiv= "cache-control" content= "no-cache" >
<meta http-equiv= "expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "this is my page" >
<!--
<link rel= "stylesheet" type= "text/css" href= "styles.css" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
-->
</head>
<body>
<jsp:usebean id= "myusers" class = "com.po.users" scope= "page" />
<h1>setproperty动作元素</h1>
<hr>
<!--根据表单自动匹配所有的属性 -->
<%--
<jsp:setproperty name= "myusers" property= "*" />
--%>
<!--根据表单匹配所有部分的属性 -->
<%--
<jsp:setproperty name= "myusers" property= "username" />
--%>
<!--根表单无关,通过手工赋值给属性 -->
<%--
<jsp:setproperty name= "myusers" property= "username" value= "lisi" />
<jsp:setproperty name= "myusers" property= "password" value= "888888" />
--%>
<!--通过url传参数给属性赋值 -->
<jsp:setproperty name= "myusers" property= "username" />
<jsp:setproperty name= "myusers" property= "password" param= "mypass" />
<!-- 使用传统的表达式方式来获取用户名和密码 -->
<%--
用户名:<%=myusers.getusername() %><br>
密码:<%=myusers.getpassword() %><br>
--%>
<!-- 使用getproperty方式来获取用户名和密码 -->
用户名:<jsp:getproperty name= "myusers" property= "username" /> <br>
密码:<jsp:getproperty name= "myusers" property= "password" /><br>
<br>
<br>
<a href= "testscope.jsp" rel= "external nofollow" >测试javabean的四个作用域范围</a>
<%
request.getrequestdispatcher( "testscope.jsp" ).forward(request, response);
%>
</body>
</html>
|
3、testscope.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
<%@ page language= "java" import = "java.util.*"
contenttype= "text/html; charset=utf-8" %>
<%@ page import = "com.po.users" %>
<%
string path = request.getcontextpath();
string basepath = request.getscheme() + "://"
+ request.getservername() + ":" + request.getserverport()
+ path + "/" ;
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" >
<html>
<head>
<base href= "<%=basepath%>" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
<title>my jsp 'testscope.jsp' starting page</title>
<meta http-equiv= "pragma" content= "no-cache" >
<meta http-equiv= "cache-control" content= "no-cache" >
<meta http-equiv= "expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "this is my page" >
<!--
<link rel= "stylesheet" type= "text/css" href= "styles.css" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
-->
</head>
<body>
<h1>javabean的四个作用域范围</h1>
<hr>
<jsp:usebean id= "myusers" class = "com.po.users" scope= "page" />
用户名:<jsp:getproperty name= "myusers" property= "username" /><br> 密码:<jsp:getproperty
name= "myusers" property= "password" /><br>
<!-- 使用内置对象获取用户名和密码 -->
<hr>
<%--
用户名:<%=((users)application.getattribute( "myusers" )).getusername()%><br>
密码:<%=((users)application.getattribute( "myusers" )).getpassword() %><br>
--%>
<%--
用户名:<%=((users)session.getattribute( "myusers" )).getusername()%><br>
密码:<%=((users)session.getattribute( "myusers" )).getpassword() %><br>
--%>
<%--
用户名:<%=((users)request.getattribute( "myusers" )).getusername()%><br>
密码:<%=((users)request.getattribute( "myusers" )).getpassword() %><br>
--%>
<%
string username = "" ;
string password = "" ;
if (pagecontext.getattribute( "myusers" ) != null ) {
username = ((users) pagecontext.getattribute( "myusers" ))
.getusername();
password = ((users) pagecontext.getattribute( "myusers" ))
.getpassword();
}
%>
用户名:<%=username%><br> 密码:<%=password%><br>
</body>
</html>
|
四 测试结果
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://cakin24.iteye.com/blog/2395384