第1个例子
useBean1.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="useBean2.jsp" method="post">
<jsp:useBean id="stu1" scope="session" class="com.Student"/>
<jsp:setProperty name="stu1" property="age" value="10000"/>
<jsp:getProperty name="stu1" property="age"/>
<%-- jsp:getProperty 表示获取属性值并显示出来--%>
<input type="submit" value="提交">
</form>
</body>
</html>
useBean2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
传递过来的值为:
<%--jsp:getProperty 表示获取属性值并显示出来--%>
<jsp:useBean id="stu1" scope="session" class="com.Student"/>
<jsp:getProperty name="stu1" property="age"/>
</body>
</html>
com.Student
package com;
public class Student {
private int sid;
private String name;
private int age;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
第2个例子
useBean3.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="useBean4.jsp" method="post">
<center>添加信息<br>
学生学号<input type="text" name="sid"><br>
学生姓名<input type="text" name="name"><br>
学生年龄<input type="text" name="age"><br>
<input type="submit" value="确定">
</center>
</form>
</body>
</html>
useBean4.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="stu2" scope="page" class="com.Student"></jsp:useBean>
<%--还有就是如果想一次设定所有参数,可以这样写 --%>
<jsp:setProperty name="stu2" property="*"/>
添加的学号是:<jsp:getProperty name="stu2" property="sid"/><br>
添加的姓名是:<jsp:getProperty name="stu2" property="name"/><br>
添加的年龄是:<jsp:getProperty name="stu2" property="age"/><br>
</body>
</html>
JSP中useBean动作
最新推荐文章于 2024-04-27 15:53:50 发布