Scope:一共有page、request、session和application 4个属性范围,默认是page。
Student类
package com.lang.model;
public class Student {
private String name;
private int age;
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;
}
}
未使用javabean的jsp
javabean01.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.lang.model.Student" %>
<!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>尚未使用javabean</title>
</head>
<body>
<%
Student student = new Student();
student.setName("王二小");
student.setAge(12);
%>
<h2>姓名:<%=student.getName() %></h2>
<h2>年龄:<%=student.getAge() %></h2>
</body>
</html>
运行结果:
使用javabean
javabean02.jsp
<%@ 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>使用javabean</title>
</head>
<body>
<jsp:useBean id="student" scope="page" class="com.lang.model.Student"></jsp:useBean>
<%
student.setName("王老大");
student.setAge(13);
%>
<h2>姓名:<%=student.getName() %></h2>
<h2>年龄:<%=student.getAge() %></h2>
</body>
</html>
运行结果: