JavaBean实例:
新建如下项目——新建包——新建类……
实例一:
一:在tsc包中新建Person类
package tsc;
public class Person {
String name = "ts";
int age = 18;
public Person() {
}
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;
}
}
二:新建jweb5.jsp,利用forward跳转页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="tsc.*"%>
<!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="person1" class="tsc.Person" scope="session"></jsp:useBean>
<jsp:setProperty property="name" name="person1" value="list" />
<jsp:setProperty property="age" name="person1" value="20" />
<jsp:forward page="jweb6.jsp" />
</body>
</html>
三:新建jewb6.jsp,获取参数值
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="tsc.*"%>
<!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>
<%!Person p1 = new Person();%>
<%
try {
p1 = (Person) session.getAttribute("person1");//id名
out.print("姓名是:" + p1.getName() + "<br>" + "年龄是:" + p1.getAge());
} catch (Exception e) {
out.print("error");
}
%>
</body>
</html>
运行结果:
注:forward跳转不会在地址栏中显示新跳转的页面;超级链接跳转在地址栏中会显示新跳转的页面。
若将提交方式改为超链接方式:<a href="jweb6.jsp">超链接</a>
运行效果: