JSP3

JavaBean

JSP3_第1张图片
JavaBean
JSP3_第2张图片
JavaBean属性

在JSP中使用JavaBean

JSP3_第3张图片
JavaBean的相关标签

初步应用JavaBean

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


    

表单提交到JSP的页面

姓名:
密码:

success.jsp

<%@page import="cn.bean.user" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


    

接受数据

<% //获取表单的内容 String username = request.getParameter("username"); String password = request.getParameter("password"); //创建user对象,set设置值 user User = new user(); User.setUsername(username); User.setPassword(password); %> <%=User.getUsername()%>

JavaBean user.java

package cn.bean;

public class user {
    private String username;
    private String password;
    
    
    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;
    }
}   

使用JSP的动作标签封装数据

JSP3_第4张图片
JSP3_第5张图片
JSP3_第6张图片

接受数据

<% //获取表单的内容 String username = request.getParameter("username"); String password = request.getParameter("password"); //创建user对象,set设置值 user User = new user(); User.setUsername(username); User.setPassword(password); %>

内省 Introspector

访问JavaBean属性的两种方式

  • 直接调用bean的setXXX或gerXXX方法
  • 通过内省的技术访问(java.beans包提供了内省的API)
    • 内省技术是基于反射技术的
    • Instrospector类获得Bean对象的BeanInfo, 然后通过BeanInfo来获取属性的描述器PropertyDescriptor, 从而获得某个属性对应的getter/setter方法,然后通过反射机制调用这些方法
/**
 * 获取请求参数,完成数据
 * @author limaoquan
 *
 */
public class UserServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /**第一种
         * //获取表单的内容
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //创建user对象,set设置值
        user User = new user();
        User.setUsername(username);
        User.setPassword(password); 
        */
        
        //第二种
        //获取输入的数据
        Map map = request.getParameterMap();
        //创建user对象
        user User = new user();
        //自己编写封装数据的方法
        try {
            populate(map,User);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        
        System.out.println(User.getUsername());
        System.out.println(User.getPassword());
    }
   /* 完成数据的封 */
private void populate(Map map, user User) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        //获取类的信息
        BeanInfo info = Introspector.getBeanInfo(User.getClass());
        //获取属性描述
        PropertyDescriptor[] pds = info.getPropertyDescriptors();
        //遍历
        for(PropertyDescriptor pd : pds){
            String name = pd.getName();//user中属性名称
            //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);
    }
}

BeanUtils工具包

  导包 commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar

BeanUtils可以自动封装String,double,int
但是封装日期类型,需要自己编写代码

  • 编写一个类,实现Converter接口,重写方法,把字符串转换成日期
  • 在封装数据之前进行注册.
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 = null;
        try {
            date = sdf.parse(dDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        return date;
    }

}
public class UserBeanUtil extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取数据
        Map map = request.getParameterMap();
        //创建user对象
        user User = new user();
        //在封装数据之前进行注册
        MyDateConverter converter = new MyDateConverter();
        ConvertUtils.register(converter, 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);
    }

}

你可能感兴趣的:(JSP3)