---
title: "jsp作业（3）--通过jsp动作元素将表单的内容和javabean的属性绑定，并显示出来。"
author: "mywebcode"
date: "2012-11-11T21:40:00Z"
tags: []
slug: "jsp-3-jsp-javabean"
draft: false
lastmod: "2013-05-08T10:12:47Z"
id: 1862410
url: "https://mywebcode.iteye.com/blog/1862410"
---

//Person.java

package com.hbsi.bean;

import java.util.Date;

public class Person {  
private String name;  
private int age;  
private String sex;  
private Date birthday;

public int getAge() {  
return age;  
}  
public void setAge(int age) {  
this.age = age;  
}

public String getName() {  
return name;  
}  
public void setName(String name) {  
this.name = name;  
}  
public String getSex() {  
return sex;  
}  
public void setSex(String sex) {  
this.sex = sex;  
}  
public Date getBirthday() {  
return birthday;  
}  
public void setBirthday(Date birthday) {  
this.birthday = birthday;  
}

}

//form.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%&gt;  
&lt;%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;base href="&lt;%=basePath%&gt;"&gt;

&lt;/head&gt;

&lt;body&gt;  
&lt;center&gt;  
&lt;form action="a3.jsp" method="post"&gt;  
姓名：&lt;input type="text" name="name"/&gt;&lt;br&gt;  
年龄：&lt;input type="text" name="age"/&gt;&lt;br/&gt;  
性别：&lt;input type="text" name="sex"/&gt;&lt;br/&gt;  
生日：&lt;input type="text" name="birthday"/&gt;&lt;br&gt;  
&lt;input type="submit" value="提交"/&gt;&nbsp;&nbsp;&lt;input type="reset" name="重置"/&gt;

&lt;/form&gt;  
&lt;/center&gt;  
&lt;/body&gt;  
&lt;/html&gt;

//a3.jsp

&lt;%@ page language="java" import="java.util.\*,java.text.\*" pageEncoding="UTF-8"%&gt;  
&lt;%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;base href="&lt;%=basePath%&gt;"&gt;

&lt;/head&gt;

&lt;body&gt;  
&lt;%  
request.setCharacterEncoding("UTF-8");

%&gt;  
&lt;br&gt;  
&lt;jsp:useBean id="person" class="com.hbsi.bean.Person" scope="page"/&gt;  
&lt;jsp:setProperty property="name" name="person" param="name"/&gt;  
&lt;jsp:setProperty property="age" name="person" param="age"/&gt;

&lt;jsp:setProperty property="sex" name="person" param="sex"/&gt;

&lt;% String date = request.getParameter("birthday");  
DateFormat df = DateFormat.getDateInstance();  
Date d1 = df.parse(date);  
%&gt;  
&lt;jsp:setProperty property="birthday" name="person" value="&lt;%=d1 %&gt;"/&gt;  
姓名：&lt;%=person.getName() %&gt;&lt;br&gt;  
年龄：&lt;%=person.getAge() %&gt;&lt;br/&gt;  
性别：&lt;%=person.getSex() %&gt;&lt;br/&gt;  
生日：&lt;%=person.getBirthday().toLocaleString() %&gt;  
&lt;/body&gt;  
&lt;/html&gt;