定义一个users类
package srever;public class Users {private String username;private String password;public Users() {}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
定义一个login的jsp页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologind.jsp?mypass=999999" name="loginForm" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</table>
</body>
</html>
定义一个dologind的jsp页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="myUsers" class="srever.Users" scope="application"/>
<h1>setproperty</h1><br>
<!-- 通过url传参数给属性赋值-->
<jsp:setProperty name="myUsers" property="username"/>
<jsp:setProperty name="myUsers" property="password" param="mypass"/>
<!-- 使用getproperty获取密码 -->
账号:<jsp:getProperty name="myUsers" property="username"/>
密码:<jsp:getProperty name="myUsers" property="password" />
<a href="testScope.jsp">测试作用域范围</a>
</body>
</html>
定义一个testscope页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="srever.Users"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Javabean的作用域</h1>
<hr>
<jsp:useBean id="myUsers" class="srever.Users" scope="application"/>
账号:<jsp:getProperty name="myUsers" property="username"/>
密码:<jsp:getProperty name="myUsers" property="password" />
<!-- 使用内置对象获取 -->
用户名:<%=((Users)application.getAttribute("myUsers")).getUsername() %><br>
密码:<%=((Users)application.getAttribute("myUsers")).getPassword() %><br></body>
</html>
运行结果
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="srever.Users"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Javabean的作用域</h1>
<hr>
<jsp:useBean id="myUsers" class="srever.Users" scope="session"/>
账号:<jsp:getProperty name="myUsers" property="username"/>
密码:<jsp:getProperty name="myUsers" property="password" />
<!-- 使用内置对象获取 -->
<hr>
用户名:<%=((Users)session.getAttribute("myUsers")).getUsername() %><br>
密码:<%=((Users)session.getAttribute("myUsers")).getPassword() %><br></body>
</html>
改为session
运行结果
改为request
dologin
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="myUsers" class="srever.Users" scope="application"/>
<h1>setproperty</h1><br>
<!-- 通过url传参数给属性赋值-->
<jsp:setProperty name="myUsers" property="username"/>
<jsp:setProperty name="myUsers" property="password" param="mypass"/>
<!-- 使用getproperty获取密码 -->
账号:<jsp:getProperty name="myUsers" property="username"/>
密码:<jsp:getProperty name="myUsers" property="password" />
<a href="testScope.jsp">测试作用域范围</a>
<%request.getRequestDispatcher("tstScope.jsp").forward(request,response);
%>
</body>
</html>
testscope
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="srever.Users"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Javabean的作用域</h1>
<hr>
<jsp:useBean id="myUsers" class="srever.Users" scope="request"/>
账号:<jsp:getProperty name="myUsers" property="username"/>
密码:<jsp:getProperty name="myUsers" property="password" />
<!-- 使用内置对象获取 -->
<hr>
用户名:<%=((Users)request.getAttribute("myUsers")).getUsername() %><br>
密码:<%=((Users)request.getAttribute("myUsers")).getPassword() %><br></body>
</html>
可以获取数值
page的话只能在当前页面获得,其他页面是无效的。
dologin的jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="myUsers" class="srever.Users" scope="page"/>
<h1>setproperty</h1><br>
<!-- 通过url传参数给属性赋值-->
<jsp:setProperty name="myUsers" property="username"/>
<jsp:setProperty name="myUsers" property="password" param="mypass"/>
<!-- 使用getproperty获取密码 -->
账号:<jsp:getProperty name="myUsers" property="username"/>
密码:<jsp:getProperty name="myUsers" property="password" />
<a href="testScope.jsp">测试作用域范围</a></body>
</html>
testscope的jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="srever.Users"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Javabean的作用域</h1>
<hr>
<jsp:useBean id="myUsers" class="srever.Users" scope="request"/>
账号:<jsp:getProperty name="myUsers" property="username"/>
密码:<jsp:getProperty name="myUsers" property="password" />
<!-- 使用内置对象获取 -->
<hr>
<%String username="";String password="";if(pageContext.getAttribute("myuser")!=null){username=((Users)pageContext.getAttribute("Users")).getUsername();password=((Users)pageContext.getAttribute("Users")).getPassword();}
%>
用户名:<%=username %>
密码:<%=password %>
</body>
</html>
运行结果