(1)在myeclipse下建立工程mySecondBean。
(2)在src目录下建立package test,然后建立Person.java
package test;
public class Person {
private String name;
private int age;
public void setName(String name)
{
this.name=name;
}
public void setAge(int age)
{
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
(3)保存后会在WebRoot/WEB-INF/classes 自动生成test/Person.class文件。
(4)在WebRoot下建立JavaBeanDemo.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=gb2312">
<title>javabeanDemo</title>
</head>
<jsp:useBean id="personInfo" scope="page" class="test.Person" />
<% personInfo.setName("sunxuejiao");
personInfo.setAge(20);%>
<body>
<h2 align="center">
显示javaBean中的信息
</h2>
<hr/>
<%=personInfo.getName()%>
<%=personInfo.getAge()%>
</body>
</html>
如图: