<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>zr.com.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>*.lf</url-pattern>
</servlet-mapping>
</web-app>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test BeanUtils</title>
</head>
<body>
<form action="BeanUtils/lf.lf" method="post">
品牌:<input name="brand" /><br>
颜色:<input name="color" /><br>
车龄:<input name="carAge" /><br>
生成日期:<input name="createDate" /><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
package zr.com.pojo;
import java.sql.Date;
/**
*
* @author LF
*
*/
public class Car {
private String brand;
private String color;
private int carAge;
private Date createDate;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getCarAge() {
return carAge;
}
public void setCarAge(int carAge) {
this.carAge = carAge;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Car() {
super();
}
public Car(String brand, String color, int carAge, Date createDate) {
super();
this.brand = brand;
this.color = color;
this.carAge = carAge;
this.createDate = createDate;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", color=" + color + ", carAge="
+ carAge + ", createDate=" + createDate + "]";
}
}
package zr.com.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import zr.com.pojo.Car;
public class TestServlet extends HttpServlet{
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 获取参数
String brand = req.getParameter("brand");
String color = req.getParameter("color");
String carAge = req.getParameter("carAge");
String createDate = req.getParameter("createDate");
System.out.println(brand+","+color+","+carAge+","+createDate);
Car car = new Car();
try {
//BeanUtils会自动转化类型
BeanUtils.setProperty(car, "brand", brand);
BeanUtils.setProperty(car, "color", color);
BeanUtils.setProperty(car, "carAge", carAge);
BeanUtils.setProperty(car, "createDate", createDate);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(car);
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}