BeanUtils实现在当前类中找到对应的方法处理请求

一、BeanUtils

BeanUtils是一个Java工具类库,它提供了一系列用于操作Java对象的工具方法。这个工具类可以方便地对Java对象进行属性的复制、属性的获取和设置、属性的类型转换等操作。BeanUtils可以帮助开发者简化代码,提高开发效率。
 

二、反射
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。

1.读取正在运行类的方法和属性
        1.Class对象
            Class c=Class.forName("co.goho.yuanyu.pojo.Student");
            Class c0=Student.class;
            Class c1=student.getClass();
        2.
            c.getConstructors();
            c.getMethods();
            c.getFields();
            c.getDeclaredMethods();


        获得名称为a,参数为int的方法
        Method m=c.getMethod("a",int.class);

        获得指定构造方法
        Constructor ctt=c.getConstructor(int.class,String.class,float.class);
        调用构造函数 创建对象
        c.newInstance(4,"张三",38.9f);


 三、实现javaweb项目

    1.步骤,创建动态web项目,配置好服务器
    2.创建RegisterServlet(urlPattern="/register")继承HttpServlet,重写doGet和doPost,所有请求交给doGet方法处理
    3.修改index.jsp文件,添加一个表单。注册User(Stirng username,String password,String realname,String tel)
    4.创建Userservlet处理所有的和账号密码相关的请求
        /user?method=register
    5.在UserServlet先读取到mehtod的值,知道用户的意图
    6.使用反射在当前类中找到对应的方法处理该请求
    7.提取公共类
 

四、代码

提取公共类


public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先读取method
        String m=req.getParameter("method");
//        根据m来找到对应的方法
        if (m!=null){
            try {
                //通过用户传递过来的method参数找到对应的方法
                Method method=this.getClass().getDeclaredMethod(m,HttpServletRequest.class,HttpServletResponse.class);
                method.invoke(this,req,resp);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

创建Userservlet处理所有的和账号密码相关的请求,.不再设置各自单独请求映射


@WebServlet(urlPatterns = "/user")
//注册  /user?method=register
//登陆  /user?method=login
//退出  /user?method=logout
//读取到method参数的值,就可以知道用户的意图
public class UserServet extends BaseServlet {

    public void register(HttpServletRequest req, HttpServletResponse resp){
        Map<String, String[]> parameterMap = req.getParameterMap();
        Set<String> set=parameterMap.keySet();
        Iterator<String> it=set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

    }
    public void showUsers(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        List<String> strings=new ArrayList<String>();
        strings.add("一二三");
        strings.add("456");
        strings.add("9981");
        req.setAttribute("strings",strings);
        req.getRequestDispatcher("show.jsp").forward(req,resp);
    }
}

五、工程截图

 注册页面,注册请求


<form method="post" action="${pageContext.request.contextPath}/register">

</form>

六、总结

前端servlet问题的解决方案
    1.继承BaseServlet
    2.servlet.service
    3.不在设置单独请求映射,在请求中添加方法参数,根据方法参数,找到对应的函数处理请求
    4.反射,通过函数的名称和函数的参数类型找到方法,并且invoke该方法
    5.继承,当一个类继承BaseServlet之后,BaseServlet中的共有方法就直接可以被子类调用
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值