首先创建HTML页面
login to request demo 3
第一种:最简单直接的封装方式
public class RequestDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
1.获取请求正文
String username = request.getParameter("username");
String password = request.getParameter("password");
String gender = request.getParameter("gender");2.将获得的参数传入创建的实体User类中(User类创建方式略)
User user = new User();
user(username, password, gender)
System.out.println(user)
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
第二种:使用反射方式封装
此种封装的使用要求是,表单标签的name属性取值,必须和实体类中定义的属性名称一致。
public class RequestDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
private void test5(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求正文名称的枚举
Enumerationnames = request.getParameterNames(); User user = new User();
System.out.println("封装前:"+user.toString());
//2.遍历正文名称的枚举
while(names.hasMoreElements()){
String name = names.nextElement();
String value = request.getParameter(name);
try{
//1.拿到User对象中的属性描述器。是谁的属性描述器:是由构造函数的第一个参数决定的。第二个参数是指定javabean的字节码
PropertyDescriptor pd = new PropertyDescriptor(name, User.class);//参数指的就是拿哪个类的哪个属性的描述器
//2.设置javabean属性的值
Method method = pd.getWriteMethod();
//3.执行方法
method.invoke(user, value);//第一个参数是指的给哪个对象,第二个参数指的是赋什么值
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println("封装后:"+user.toString());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
注意: 在执行invoke方法时,如果得到的参数不止一个时 ,要进行转型 *** 作method.invoke(user, (object)value)。
第三种:使用apache的commons-beanutils实现封装
它里面有一个开源工具包集合commons,里面有很多开源工具类,今天我们就来讲解第一个:commons-beanutils。
实现代码:
用流的形式读取请求信息(不常用)
private void test8(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Users user = new Users();
System.out.println("封装前:"+user.toString());
try{
BeanUtils.populate(user, request.getParameterMap());//就这一句话
}catch(Exception e){
e.printStackTrace();
}
System.out.println("封装后:"+user.toString());
}
public class RequestDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求正文的字节输入流
ServletInputStream sis = request.getInputStream();
//2.读取流中的数据
int len = 0;
byte[] by = new byte[1024];
while((len = sis.read(by)) != -1){
System.out.println(new String(by,0,len));
}
}public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}}
public class RequestDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求正文的字符输入流
BufferedReader br = request.getReader();
//2.读取流中的数据
String line;
while ((line = br.readLine())!=null) {
System.out.println(line);
}
}public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)