strust2 用注入的方式获取Servlet API
一、什么是Servlet API?
Servlet API 中主要接口及实现类,包括:
- 与Servlet实现相关的Servlet接口
- Servlet接口
- GenericServlet抽象类
- HttpServlet抽象类
- 与请求和响应相关的接口
- ServletRequest接口
- ServletResponse接口
- HttpServletResquest接口
- HttpServletResponse接口
- 与Servlet配置相关的接口
- ServletConfig接口
- Servlet上下文
- ServletContext接口
- 请求转发
- RequestDispatcher接口
关于接口中的一些具体的方法可以查阅API文档,重点理解其原理。
参考:https://blog.****.net/qq_34594236/article/details/78345478?utm_source=copy
二、strust2 获取Servlet API的意义
使得strust2可以处理从网站前端获得的数据(比如前端登陆页面输入的账号密码)
这里的Servlet API主要是指的是获取request response session...对象
三、strust2 如何用注入的方式获取Servlet API
Struts2框架在运行时,请求会被StrutsPrepareAndExecuteFilter拦截,会根据请求,去 strtus.xml文件中查找到匹配的action,在action执行前,会走一些interceptor(默认执行的拦截器是struts-default.xml文件中定义的),在默认执行的拦截器中有一个servletConfig,它会判断你是否实现某个接口,如果实现了,它会把该接口对应的对象注入给你。
接口与对象的对应关系如下:
ServletRequestAware, 实现这个接口可以获取HttpServletRequest
ServletResponseAware ,实现这个接口可以获取HttpServletResponse
ServletContextAware 实现这个接口可以获取ServletContext
参考:
https://www.jianshu.com/p/239868205bcc
四、补充,其他获得API的方法
https://www.cnblogs.com/fingerboy/p/5186149.html