login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>表单提交到JSP的页面</h4>
<form action="/day12/bean/success.jsp" method="POST">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
<input type="submit" value="登陆"/>
</form>
<h4>表单提交到Servlet程序</h4>
<form action="/day12/user" method="POST">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
<input type="submit" value="登陆"/>
</form>
<h4>表单提交到Servlet(BeanUtils)程序</h4>
<form action="/day12/userBeanUtil" method="POST">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
余额:<input type="text" name="money" /><br/>
生日:<input type="text" name="birthday" /><br/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>
package cn.itcast.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;
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 org.apache.commons.beanutils.ConvertUtils;
import cn.itcast.utils.MyDateConverter;
import cn.itcast.vo.User;
/**
* 使用BeanUtils完成数据的封装
* @author Administrator
*
*/
public class UserBeanUtilServlet extends HttpServlet {
private static final long serialVersionUID = 3625882115495534032L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取数据
Map<String, String []> map = request.getParameterMap();
// 创建User对象
User user = new User();
// 完成注册
ConvertUtils.register(new MyDateConverter(), Date.class);
// 完成封装
try {
BeanUtils.populate(user, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 打印
System.out.println(user.getUsername());
System.out.println(user.getPassword());
System.out.println(user.getMoney());
System.out.println(user.getBirthday());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package cn.itcast.servlet;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.vo.User;
/**
* 获取请求参数,封装数据
* @author Administrator
*
*/
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 6390620317553505800L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取请求参数,创建User对象,设置值。
/**
*
* // 获取表单的内容
String username = request.getParameter("username");
String password = request.getParameter("password");
// 创建User对象,set设置值
User user = new User();
user.setUsername(username);
user.setPassword(password);
*/
// 获取输入的数据
Map<String, String []> map = request.getParameterMap();
// 创建User对象
User user = new User();
// 自己编写封装数据的方法
try {
populate(map,user);
} catch (Exception e) {
e.printStackTrace();
}
// 完成数据封装
System.out.println(user.getUsername());
System.out.println(user.getPassword());
}
/**
* 完成的数据
* @param map
* @param user
* @throws Exception
*/
private void populate(Map<String, String[]> map, User user) throws Exception {
BeanInfo info = Introspector.getBeanInfo(user.getClass());
// 获取属性的描述
PropertyDescriptor [] pds = info.getPropertyDescriptors();
// 循环遍历
for (PropertyDescriptor pd : pds) {
// 获取到属性的名称
String name = pd.getName();
// map的key
if(map.containsKey(name)){
// 获取属性的写的方法
Method m = pd.getWriteMethod();
// 执行之
m.invoke(user, map.get(name)[0]);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package cn.itcast.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
/**
* 字符串转换日期类
* @author Administrator
*
*/
public class MyDateConverter implements Converter{
/**
* 字符串转换成日期
*/
public Object convert(Class clazz, Object obj) {
// 把输入的字符串,转换成日期类型,返回
String dDate = (String) obj;
// 把字符串转换成日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = sdf.parse(dDate);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException("转换日期错误");
}
return date;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<!-- 配置全局初始化参数 -->
<context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
<!-- 配置全局错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
<servlet>
<servlet-name>SessionDemo1</servlet-name>
<servlet-class>cn.itcast.session.SessionDemo1</servlet-class>
</servlet>
<servlet>
<servlet-name>SessionDemo2</servlet-name>
<servlet-class>cn.itcast.session.SessionDemo2</servlet-class>
</servlet>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>cn.itcast.servlet.UserServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>UserBeanUtilServlet</servlet-name>
<servlet-class>cn.itcast.servlet.UserBeanUtilServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionDemo1</servlet-name>
<url-pattern>/session1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SessionDemo2</servlet-name>
<url-pattern>/session2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/user</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserBeanUtilServlet</servlet-name>
<url-pattern>/userBeanUtil</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>