MVC 设计模式概览
实现 MVC(Model、View、Controller) 模式的应用程序由 3 大部分构成:
模型(POJO(Plain Old Java Object)):封装应用程序的数据和业务逻辑
视图(JSP):实现应用程序的信息显示功能
控制器(Servlet、Filter):接收来自用户的输入,调用模型层,响应对应的视图组件
用户注册案例-使用 Filter 作为控制器
1. 业务流
2. 添加JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>首页</title> </head> <body> <h2><a href="register-user.action">注册用户</a></h2><hr/> <p>使用filter来作为前端控制器,代替servlet.</p> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>注册</title> </head> <body> <h2>注册</h2><hr/> <form action="submit-user.action" method="POST"> 用户名: <input type="text" name="username"><br/> 密码: <input type="password" name="password1"><br/> 重复密码:<input type="password" name="password2"><br/> <input type="reset" value="重置"> <input type="submit" value="提交"> </form> <hr/> <a href="index.jsp" style="float:right;">返回首页</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>查看</title> </head> <body> <h2>注册成功</h2> <hr/> <p>用户名:${requestScope.account.username}</p> <p>密码:${requestScope.account.password}</p> <hr/> <a href="index.jsp" style="float:right;">返回首页</a> </body> </html>
3. 添加Filter控制器
package org.rabbitx.web.filter.register; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FilterController implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException { HttpServletRequest httpReq = (HttpServletRequest)req; HttpServletResponse httpResp = (HttpServletResponse)resp; String servletPath = httpReq.getServletPath(); String dispatcherPath = null; String redirectPath = null; if(servletPath.contains("register-user")) { dispatcherPath = "register.jsp"; } else if(servletPath.contains("submit-user")) { String username = httpReq.getParameter("username"); String password1 = httpReq.getParameter("password1"); String password2 = httpReq.getParameter("password2"); if(username.isEmpty() || password1.isEmpty() || !password1.equals(password2)) { redirectPath = "register.jsp"; } else { dispatcherPath = "view.jsp"; httpReq.setAttribute("account", new Account(username,password1)); } } if(dispatcherPath != null) { httpReq.getRequestDispatcher(dispatcherPath).forward(httpReq, httpResp); } else if(redirectPath != null) { httpResp.sendRedirect(redirectPath); } else { chain.doFilter(req, resp); } } @Override public void init(FilterConfig arg0) throws ServletException { } }
4. 配置Filter控制器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <filter> <filter-name>filterController</filter-name> <filter-class>org.rabbitx.web.filter.register.FilterController</filter-class> </filter> <filter-mapping> <filter-name>filterController</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!--默认页面配置--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
使用 Filter 作为控制器的好处
使用一个过滤器来作为控制器, 可以方便地在应用程序里对所有资源(包括静态资源)进行控制访问.
Servlet和Filter比较
Servlet 能做的 Filter 都可以完成。
Filter 能做的 Servlet 不一定可以完成。 拦截资源却不是 Servlet 所擅长的! Filter 中有一个 FilterChain,这个 API 在 Servlet 中没有!