输入参数之后就直接提示某某方法不存在,可我明明已经写了相对应的方法了.

protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String methodName = request.getParameter("method");

// System.out.println(methodName+"======");
if (methodName == null || methodName.trim().isEmpty()){
throw new RuntimeException("请传入参数");
}

    Class<?> clazz = this.getClass();

// System.out.println(clazz);
Method m = null;
try {
m = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
System.out.println(m.getParameterCount());
} catch (Exception e) {
throw new RuntimeException("没有查询到" + methodName + ", 这个方法");
}
try {
m.invoke(this, request,response);//反射调用,相当于 this.addUser(request,response);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
System.out.println("您调用的" + methodName + "方法内部出现异常");
throw new RuntimeException(e);
}
}

protected void addUser(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    System.out.println("addUser()...");
}

clazz.getMethod 错了

java反射时要注意方法的修饰符也会影响getMethod的返回,它只会返回public的父类方法及自己的方法;getDeclaredMethod只会返回自己类中定义的public方法。