Servlet请求转发到MVC
来源:互联网 发布:冯小刚 王思聪 知乎 编辑:程序博客网 时间:2024/06/05 07:33
我们已经知道,来自tomcat的 Request 请求最终到达 Wrapper 容器,也就最终进入 Servlet ,而进入Servlet后必然要执行service方法,然后执行doXXX()。
但现在的 web 应用很少有直接将交互全部页面都用 servlet 来实现,而是采用更加高效开发web的 MVC 框架来实现。这些 MVC 框架基本的原理都是将所有的请求都映射到一个 Servlet,然后去实现 service 方法,这个方法也就是 MVC 框架的入口。
如何扩展?
//抽象Action
public interface Action { //在这里或者具体实现类中我们可以灵活的传很多想要的值,比如strut2中用到的ActionForm public String execute(HttpServletRequest request,ActionForm actionForm, HttpServletResponse response) throws Exception; //或者 public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception; } public class DemoServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); String path = requestURI.substring(requestURI.indexOf("/",1),requestURI.indexOf(".")); if("/login".equals(path)){ action = new LoginAction(); }else if("/addUser".equals(path)){ action = new SysUserAction(); }else{ throw new RuntimeException("请求不匹配"); } //抽象Action ActionMapping actionMapping =(ActionMapping)map.get(path); // 采用反射动态实例化Action Action action = (Action)class.forName(type).newInstance(); String forward =""; try { forward= action.execute(request, response); } catch (Exception e) { e.printStackTrace(); } request.getRequestDispatcher(forward).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } //自己的Actionpublic class LoginAction implements Action { public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception { 。。。省略 return "/success.jsp"; } }
这里我们发现,在DemoServlet 类中,存在大量的if和else,而当我们添加自己的Action时,就需要修改if/else,这就不符合对扩展开发,对修改关闭原则,这时我们想到了配置文件,用反射进行配置。
<action-config> <action path ="/login" type = "com.wybqq.servlet.LoginAction "> <forward name = "success">success.jsp</forward> <forward name = "error">error.jsp</forward> </action> 。。。省略 </action-config>
这时,我们通过创建actionMapping对象,采用反射来动态实例化Action,自动将Servlet请求转移到我们的loginAtion中。
。。。省略 ActionMapping actionMapping =(ActionMapping)map.get(path); // 采用反射动态实例化Action Action action = (Action)class.forName(type).newInstance(); String forward = action.execute(request.response); request.getRequestDispatcher(forward).forward(request, response); 。。。省略
0 0
- Servlet请求转发到MVC
- servlet request请求转发
- Servlet ---- RequestDispatcher请求转发
- servlet请求转发
- Servlet------RequestDispatcher请求转发
- servlet 转发请求
- Servlet中 请求转发:
- servlet实现请求转发(mvc设计模式)与请求重定向问题
- servlet 转发到 struts2
- Servlet请求转发 RequestDispatcher接口
- Servlet请求转发RequestDispatcher接口
- Servlet请求转发之RequestDispatcher
- Servlet请求转发 RequestDispatcher接口
- Servlet请求转发 RequestDispatcher接口
- Servlet中实现请求转发
- Servlet请求转发:RequestDispatcher接口
- Servlet请求转发与包含
- Servlet请求转发之RequestDispatcher
- Java中如何比较两个数的大小
- Git操作
- memory matlab/simulink
- Nginx实战(一) 虚拟主机
- C++构造函数析构函数
- Servlet请求转发到MVC
- Linux设备驱动第四天(自动创建设备节点、LED驱动程序)
- php-fpm死机解决办法,脚本后台自动重启
- C++中的四种类型转换
- Hibernate注解的问题
- 实现全局的Context
- VTK修炼之道10:可视化管道的连接与执行
- Codeforces Round #381 (Div. 2)-C. Alyona and mex
- 搞笑点滴