老师,这是为什么呢,查不出来了

public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
/*
.do
/login.do DefaultController login
/Cake/detail.do CakeController detail
/admin/Cake/add.do CakeController add
*/
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
String path = request.getServletPath();
/*如果请求是/admin开头的,则先截取/admin以后的字符,如果不是,则截取/后的字符*/
if(path.indexOf("admin")!=-1){
path = path.substring(7);
}else{
path = path.substring(1);
}
/*
login.do DefaultController login
Cake/detail.do CakeController detail
Cake/add.do CakeController add
*/
int index = path.indexOf("/");
String className =null;
String methodName =null;
/*
以/为分割线,截取/之前的字符,
例如Cake/add.do 截取的是例如Cake,
并拼接上Controller,以及包路径
如果没有/,则进入到默认的DefaultController中
*/
if(index!=-1){
className = "com.shop.icake.controller."+path.substring(0,index)+"Controller";
/* 从/后边的字符串,截止到.do 之前的字符串,该字符串为方法名*/
methodName = path.substring(index+1,path.indexOf(".do"));
}else{
className = "com.shop.icake.controller.DefaultController";
methodName = path.substring(0,path.indexOf(".do"));
}
try {
/*使用反射获取到Class对象*/
Class cla = Class.forName(className);
/* 创建该对象的实例对象*/
Object object = cla.newInstance();
/* 通过反射的方式执行方法*/
Method method = cla.getMethod(methodName,HttpServletRequest.class, HttpServletResponse.class);
method.invoke(object,request,response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
正在回答
同学你好

同学的这个类名写的和控制器中的不同,同学这里第三个字母写的是l,而请求的是时候请求的是t

同学这里要保持一致哦
如果我的回答解决了你的问题,请采纳,祝学习愉快.
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星