javaweb学习之filter过滤器
如图中所示,
①当网页传输一些垃圾信息时,在web服务器向后天的servlet发送请求的中间加一个过滤器,就可以减少servlet处理的压力。
②当我们在servlet中需要个前台显示数据库的信息时,将中文放在域中传给前台时,前台在解析的时候,就会发生乱码的问题,之前的解决方法时,每次在创建一个servlet的时候,首先就是设置编码,防止乱码。但是,当很多的servlet需要编解码的话,就可以在进入servlet之前先进入过滤器,解决乱码的问题
//从tomcat传过来的数据进行解码
request.setCharacterEncoding("utf-8");
//给浏览器的一个响应,让浏览器用utf-8进行解码
response.setContentType("test/html;charset=utf-8");
2.构成过滤器的步骤:
乱码过滤器的实现:
-
导包(maven中导入,jsp,servlet,jstl,standard,mysql)
-
创建一个过滤器实现filter接口
import javax.servlet.*;//导包注意 import javax.servlet.annotation.WebFilter; import java.io.IOException; //当value设置为/servlet/*时,表示所有servlet配的路径为/*都要走过滤器 @WebFilter(filterName = "demoFilter",value = "/filter") public class demoFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { //处理中文乱码的code req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); // 链 控制放流的作用,如果没有cahin那么代码就会停止在这里 chain.doFilter(req, resp); } public void init(FilterConfig config) throws ServletException { } }
编写filter的小结:
1.当value设置为/servlet/**时或者在filtermap中配置为 /servlet/*,表示所有servlet配的路径为 /servlet/*都要走过滤器。
2.chain保证了请求和响应都会走过过滤器
3.过滤器的和web服务器(tomcat)同生共死,即就是,当tomcat服务器关闭时,会执行destory的方法,当tomcat服务器开启时,会执行init的方法中的代码。
案例演示:
需求,模拟一个登录页面,设置登录正确的用户名,当登录正确后跳转到一个登陆成功的页面,并且给登录页面添加一个过滤器,结局中文乱码和防止非正常访问登录成功页面。
创建一个登录页面
login.jsp<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/login.do" method="post"> <input type="text" name="username"/> <input type="submit" value="提交"/> </form> </body> </html>
写一个登录的servlet
/login.do@WebServlet(name = "loginServlet",value = "/login.do") public class loginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); //如果用户输入的username是admin if (username.equalsIgnoreCase("admin")){ //将sessionid放在里面,好让过滤器判断是否为空 request.getSession().setAttribute("sessionid",request.getSession().getId()); //放在session域中的东西,内部转发和重定向都可以获取到 response.sendRedirect("/sys/success.jsp"); }else { //如果用户输入username不是admin request.getRequestDispatcher("login.jsp").forward(request,response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
写一个解决中文乱码的过滤器
/ccodefilter@WebFilter(filterName = "CcodeFilter") public class CcodeFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); chain.doFilter(req, resp); } public void init(FilterConfig config) throws ServletException { } }
web.xml
<filter> <filter-name>ccodefilter</filter-name> <filter-class>com.g.filter.CcodeFilter</filter-class> </filter> <filter-mapping> <filter-name>ccodefilter</filter-name> <servlet-name>loginServlet</servlet-name> </filter-mapping>
写一个登录成功的页面
success.jsp放在sys的文件下面<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登录成功</h1> <a href="/logout.do">退出</a> </body> </html>
创建一个防止非正常登录成功页面的过滤器
@WebFilter(filterName = "jspFilter") public class jspFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { //使用多态 HttpServletRequest request= (HttpServletRequest) req; HttpServletResponse response= (HttpServletResponse) resp; Object sessionid = request.getSession().getAttribute("sessionid"); //判断存放在session域中的数据是否为空 chain.doFilter(req, resp); if (sessionid == null) { //直接跳转到登录页面 //req.getRequestDispatcher("/logout.do").forward(request,response); resp.setContentType("text/html;charset=utf-8"); //三秒提醒后跳转到登录页面 response.getWriter().write("非正常登录正在跳转到登录页面"); response.setHeader("refresh","3;url=/logout.do"); } request.getSession().removeAttribute("sessionid"); } public void init(FilterConfig config) throws ServletException { } }
<filter> <filter-name>jspfilter</filter-name> <filter-class>com.g.filter.jspFilter</filter-class> </filter> <filter-mapping> <filter-name>jspfilter</filter-name> <url-pattern>/sys/*</url-pattern> </filter-mapping>
创建一个servlet用来退出
/logout.do servlet@WebServlet(name = "logoutServlet",value = "/logout.do") public class logoutServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getSession().getAttribute("sessionid")); request.getRequestDispatcher("login.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }