jsp:getProperty 获取 javabean
<jsp:getProperty property="属性名称" name="实例化对象的名称"/>
<jsp:useBean id="student" scope="page" class="com.ruanku.model.Student"/>
<%
student.setName("王二小2");
student.setAge(12);
%>
<h1>姓名:<jsp:getProperty property="name" name="student"/></h1>
<h1>年龄:<jsp:getProperty property="age" name="student"/></h1>javabean 的保存范围
Javabean 的保存范围有 page,request,session.application,默认是 page;
request
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" scope="request" class="com.ruanku.model.Student"/>
<jsp:setProperty property="name" name="student" value="陈晨大爷"/>
<jsp:setProperty property="age" name="student" value="22"/>
<jsp:forward page="target01.jsp" />
</body>
</html><%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" scope="request" class="com.ruanku.model.Student"/>
<h1>姓名:<jsp:getProperty property="name" name="student"/></h1>
<h1>年龄:<jsp:getProperty property="age" name="student"/></h1>
</body>
</html>session
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" scope="session" class="com.ruanku.model.Student"/>
<jsp:setProperty property="name" name="student" value="陈晨大爷"/>
<jsp:setProperty property="age" name="student" value="22"/>
<h1>session值设置完毕!</h1>
</body>
</html><%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>session中取值!</h1>
<jsp:useBean id="student" scope="session" class="com.ruanku.model.Student"/>
<h1>姓名:<jsp:getProperty property="name" name="student"/></h1>
<h1>年龄:<jsp:getProperty property="age" name="student"/></h1>
</body>
</html>application
同上 区别不大 不同浏览器也可以访问。
javabean 删除
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
session.removeAttribute("student");
%>
<h1>清除了session的值!</h1>
</body>
</html>

本文介绍了如何使用JSP操作JavaBean,包括设置属性、获取属性等,并详细展示了不同作用域下JavaBean的使用方法及如何清除session中的JavaBean。
1126

被折叠的 条评论
为什么被折叠?



