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()...");
}