内省和BeanUtils框架各种表单输入域数据的获取及中文编码
注: 需要jar包: commons-beanutils-1.8.3.jar和commons-logging-1.1.1.jar
表单输入域类型:
radio checkbox,即使表单中有对应名称输入域,如果一个不选择,则什么数据不会带给服务器。(注意空指针异常)
如果选择了其中的一个或多个,则把他们的value的取值提交给服务器。
如果选择了其中的一个或多个,他们又没有value取值,则提交给服务器的值是on.
*********************************************************************************************
public class Student {
private int id;
private String name;
private String password;
private String gender;
private String married;
private String[] hobby;
private String city;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getMarried() {
return married;
}
public void setMarried(String married) {
this.married = married;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Student [city=" + city + ", description=" + description
+ ", gender=" + gender + ", hobby=" + Arrays.toString(hobby)
+ ", id=" + id + ", married=" + married + ", name=" + name
+ ", password=" + password + "]";
}
}
*********************************************************************************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>学生信息表</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/day07/servlet/RequestDemo4" method="post">
<input type="hidden" name="id" value="438" /> <br/><br/>
姓名:<input type="text" name="name" /> <br/><br/>
密码:<input type="password" name="password" /> <br/><br/>
性别:<input type="radio" name="gender" value="1" />男
<input type="radio" name="gender" value="0" />女 <br/><br/>
已婚: <input type="checkbox" name="married" /> <br/><br/>
爱好:<input type="checkbox" name="hobby" value="fb" />足球
<input type="checkbox" name="hobby" value="bb" />篮球
<input type="checkbox" name="hobby" value="tt" />乒乓球 <br/><br/>
故乡:<select name="city">
<option value="BJ">北京</option>
<option value="SD">山东</option>
<option value="SH">上海</option>
</select> <br/><br/>
简介:<textarea name="description" rows="5" cols="40" ></textarea> <br/><br/>
<input type="image" src="submit.png" /> <br/><br/>
</form>
</body>
</html>
*********************************************************************************************
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 cn.itcst.domain.Student;
public class RequestDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// HTML采用什么编码,此处设置为什么编码
request.setCharacterEncoding("UTF-8");
Student s = new Student();
System.out.println("封装前:" + s);
try {
BeanUtils.populate(s, request.getParameterMap());
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("封装后:" + s);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
*********************************************************************************************
注: 需要jar包: commons-beanutils-1.8.3.jar和commons-logging-1.1.1.jar
表单输入域类型:
radio checkbox,即使表单中有对应名称输入域,如果一个不选择,则什么数据不会带给服务器。(注意空指针异常)
如果选择了其中的一个或多个,则把他们的value的取值提交给服务器。
如果选择了其中的一个或多个,他们又没有value取值,则提交给服务器的值是on.
*********************************************************************************************
public class Student {
private int id;
private String name;
private String password;
private String gender;
private String married;
private String[] hobby;
private String city;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getMarried() {
return married;
}
public void setMarried(String married) {
this.married = married;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Student [city=" + city + ", description=" + description
+ ", gender=" + gender + ", hobby=" + Arrays.toString(hobby)
+ ", id=" + id + ", married=" + married + ", name=" + name
+ ", password=" + password + "]";
}
}
*********************************************************************************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>学生信息表</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/day07/servlet/RequestDemo4" method="post">
<input type="hidden" name="id" value="438" /> <br/><br/>
姓名:<input type="text" name="name" /> <br/><br/>
密码:<input type="password" name="password" /> <br/><br/>
性别:<input type="radio" name="gender" value="1" />男
<input type="radio" name="gender" value="0" />女 <br/><br/>
已婚: <input type="checkbox" name="married" /> <br/><br/>
爱好:<input type="checkbox" name="hobby" value="fb" />足球
<input type="checkbox" name="hobby" value="bb" />篮球
<input type="checkbox" name="hobby" value="tt" />乒乓球 <br/><br/>
故乡:<select name="city">
<option value="BJ">北京</option>
<option value="SD">山东</option>
<option value="SH">上海</option>
</select> <br/><br/>
简介:<textarea name="description" rows="5" cols="40" ></textarea> <br/><br/>
<input type="image" src="submit.png" /> <br/><br/>
</form>
</body>
</html>
*********************************************************************************************
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 cn.itcst.domain.Student;
public class RequestDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// HTML采用什么编码,此处设置为什么编码
request.setCharacterEncoding("UTF-8");
Student s = new Student();
System.out.println("封装前:" + s);
try {
BeanUtils.populate(s, request.getParameterMap());
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("封装后:" + s);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
*********************************************************************************************