将前台表单中的数据提交给后台
HTML代码区
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="ServletDemo3" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
性别:
女<input type="radio" name="gender" value="女">
男<input type="radio" name="gender" value="男" checked="checked">
<br>
年龄:<input type="text" name="age"><br>
出生年月:<input type="text" name="birthday"><br>
爱好:
吃饭:<input type="checkbox" name="hobby" value="吃饭">
睡觉:<input type="checkbox" name="hobby" value="睡觉" checked="checked">
运动:<input type="checkbox" name="hobby" value="运动">
<br>
地址:<select name="address">
<option>成都</option>
<option>北京</option>
<option>上海</option>
<option>广东</option>
</select>
<br>
备注:<br><textarea rows="20" cols="35" name="remark"></textarea><br>
<input type="submit" value="注册">
</form>
</body>
</html>
JavaBean
package com.java.domain;
import java.util.Arrays;
import java.util.Date;
/*
* 使用beanutils的前提条件是网页上的名字要与后台对象属性名一致
* BeanUtils中基础数据类型它会自动转换
*
*
*/
public class User {
private String username;
private String password;
private String gender;
private int age;
private Date birthday;
// private String birthday;
private String hobby;
// private String[] hobby;
private String address;
private String remark;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
// public String[] getHobby() {
// return hobby;
// }
// public void setHobby(String[] hobby) {
// this.hobby = hobby;
// }
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// public String getBirthday() {
// return birthday;
// }
// public void setBirthday(String birthday) {
// this.birthday = birthday;
// }
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", gender=" + gender + ", age=" + age
+ ", birthday=" + birthday + ", hobby=" + hobby + ", address=" + address + ", remark=" + remark + "]";
}
// @Override
// public String toString() {
// return "User [username=" + username + ", password=" + password + ", gender=" + gender + ", age=" + age
// + ", birthday=" + birthday + ", hobby=" + Arrays.toString(hobby) + ", address=" + address + ", remark="
// + remark + "]";
// }
}
Servlet代码区
package com.java.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import com.java.domain.User;
/*
* 用BeanUtils的方式接收:作用:可以快速的将前台接收到的数据封装到对象中
*
* 基础数据类型会自动转换
* 日期型需要使用转换器
*
*/
@WebServlet("/ServletDemo3")
public class ServletDemo3 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求的编码
request.setCharacterEncoding("utf-8");
//new一个封装对象,取出toString方法
User user = new User();
try {
//用自带的转换器处理Date格式 //注册一个转换器 字符串转换为日期 格式只能为 ‘yyyy-MM-dd’
//ConvertUtils.register(new DateLocaleConverter(), Date.class);
//自定义转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class arg0, Object value) {
//判断value的值是否为空,为空则返回null
if(value==null) {
return null;
}
//取反 value的值是否为String
if(!(value instanceof String)) {
//return null;
//抛出异常
throw new ConversionException("conversion error");
}
String date = (String) value;
//判断输入的是一串空字符串就返回null trim() 方法用于删除字符串的头尾空白符
if(date.trim().equals(" ")) {
return null;
}
String format = null;
//contains:当此字符串包含指定的char值时才返回true,否则false
if(date.contains("/")){
format = "yyyy/MM/dd";
}
if(date.contains("-")){
format = "yyyy-MM-dd";
}
if(date.contains("年")){
format = "yyyy年MM月dd日";
}
//定义时间格式
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
//字符格式转日期格式
return sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}, Date.class);
//BeanUtils可以快速的将前台接收的数据封装到对象中。
BeanUtils.populate(user, request.getParameterMap());
//把数组转换成字符串(复选框的值就是数组;注意:多选按钮可以使用数组接收也可以使用字符串接收Arrays.toString)
String[] hobby = request.getParameterValues("hobby");
user.setHobby(Arrays.toString(hobby));
//System.out.println(Arrays.toString(user.getHobby()));
System.out.println(user);
} catch (IllegalAccessException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
演示结果