黑马程序员技术交流社区
标题:
//做框架时吧用户填写的数据封装到JavaBean中
[打印本页]
作者:
tfy
时间:
2012-12-23 01:03
标题:
//做框架时吧用户填写的数据封装到JavaBean中
package com.itheima.request;
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 com.itheima.domain.User;
public class RequestDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
test4(request);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//做框架时吧用户填写的数据封装到JavaBean中
public void test4(HttpServletRequest request) throws Exception{
Map map=request.getParameterMap();
User user=new User();
System.out.println("封装前的数据"+user);
for(Object o:map.entrySet() ){
Map.Entry me=(Map.Entry)o;
String paramName=(String) me.getKey();
String[] values=(String[]) me.getValue();
System.out.println(paramName);
//吧得到的数据封装到javaBean中:内省
PropertyDescriptor pd=new PropertyDescriptor(paramName, User.class);
Method m=pd.getWriteMethod();
if(values.length>1){
m.invoke(user,(Object) values);
m.invoke(user,new Object[]{ values});
}else{
m.invoke(user, values);
}
}
System.out.println("封装后的数据"+user);
}
public void test3(HttpServletRequest request){
Map map=request.getParameterMap();
for(Object o:map.entrySet() ){
Map.Entry me=(Map.Entry)o;
System.out.println(me.getKey()+"="+me.getValue());
}
}
private void test2(HttpServletRequest request) {
String username=request.getParameter("username");
// String password=request.getParameter("password");
String pass[]=request.getParameterValues("password");
if(pass!=null && pass.length>0){
for(String s:pass){
System.out.println(s);
}
}
String code=request.getParameter("code");
System.out.println(username+"\t"+code);
}
private void test1(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
String code=request.getParameter("code");
System.out.println(username+"\t"+password+"\t"+code);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
作者:
济南王昭珽
时间:
2012-12-25 10:43
{:soso_e179:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2