JavaBeanDemo.java文件:
<%@ page language="java" import="java.util.*,com.neu.domain.Person" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>在jsp中使用JavaBean</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="p" class="com.neu.domain.Person" scope="page"></jsp:useBean>
<jsp:setProperty property="name" name="p" value="李四"/>
<jsp:setProperty property="age" name="p" value="27"/>
<jsp:setProperty property="birthday" name="p" value="<%=new Date() %>"/>
<jsp:getProperty property="name" name="p"/>
<jsp:getProperty property="age" name="p"/>
<jsp:getProperty property="birthday" name="p"/>
</body>
</html>
Person.java文件:
package com.neu.domain;
import java.io.Serializable;
import java.util.Date;
public class Person implements Serializable {
private String name;
private int age;//基本类型自动转换
private Date birthday;
public Person() {
super();
}
public Person(String name, int age, Date birthday) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
}
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
运行:http://localhost:8080/JspProject/javaBeanDemo.jsp
结果:李四 27 Sat Sep 03 19:41:45 CST 2016