为什么核心控制器这样写报错?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package com.imooc.sm.global; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @WebServlet (name = "DispatcherServlet" ) public class DispatcherServlet extends HttpServlet { private ApplicationContext context; public void init() throws ServletException { super .init(); context = new ClassPathXmlApplicationContext( "spring.xml" ); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super .service(req, resp); /**Servlet核心控制器需要处理的请求url * /staff/add.do login.do * staffController * public void add(HttpServletRequest request,HttpServletResponse response){ * * } */ String path = req.getServletPath().substring( 1 ); String beanName = null ; String methodName = null ; int index = path.indexOf( '/' ); if (index != - 1 ) { beanName = path.substring( 0 ,index)+ "Controller" ; methodName = path.substring(index+ 1 ,path.indexOf( ".do" )); } else { beanName = "selfController" ; methodName = path.substring( 0 ,path.indexOf( ".do" )); } Object obj = context.getBean(beanName); try { Method method = obj.getClass().getMethod(methodName,HttpServletRequest. class ,HttpServletResponse. class ); method.invoke(obj,req,resp); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } 老师,为什么我直接让Servlet继承HTTPServlet会报反射 405 的错误,但是按照老师那样写就不会报错呢??这个把request和response 强转为Http类型的和直接使用HttpServlet的request对象在反射的时候有什么不同呢??? |
正在回答
这个是HttpServlet中可以重写的方法,同学可以看到通过传参的不同,重载的方法也是不同的。
反射失败应该和这个也有关系,建议同学按照老师的方法,选择底下的方法,ServletRequest和ServletResponse为参数的方法。
void service(ServletRequest, ServletResponse)方法是重写的其父类GenericServlet类的方法,这个方法是公共的(public),其作用是接受客户端的请求并将其传递给protected void service(HttpServletRequest, HttpServletResponse)方法;
protected void service(HttpServletRequest, HttpServletResponse)方法是HttpServlet类定义的方法,是受保护的(protected),主要作用是接受标准的Http请求(HttpServletRequest),并根据请求方式不同分发到不同的doXXX(HttpServletRequest, HttpServletResponse)方法
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
将你的代码粘贴到老师的源码中,会出现下图报错,我测试了老师的源码,即使继承HttpServlet也是不会报错,建议同学将你的报错提示粘贴一下,方便我们具有针对性的为你解答,祝学习愉快~
同学问的是下图代码吗?为什么要强转成HttpServletRequest和HttpServletResponse类型的。
如果是,你可以参考一下这个被采纳的问答,如果还有问题,可以继续提问哦,祝学习愉快~
http://class.imooc.com/course/qadetail/74259
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧