1、filter 过滤器的概述
filter过滤器:是面向切面编程的一种实现策略,在不影响原来的程序流程的前提下,将一些业务逻辑切入流程中,在请求达到目标之前进行处理,一般用于编码过滤、权限过滤、....
2、filter 过滤器的创建步骤
1、创建一个普通的java类实现filter接口
2、实现接口中的三个方法(init,doFilter,destory)
3、配置过滤器(web.xml)
3、实例_编码过滤器
1 package com.shore.a_filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 12 public class CharsetEncoding implements Filter{ 13 private String encoding; 14 public void init(FilterConfig config) throws ServletException {//过滤器初始化 15 encoding = config.getInitParameter("encoding");//获取web.xml配置文件中的参数 16 } 17 18 public void doFilter(ServletRequest request, ServletResponse response, 19 FilterChain chain) throws IOException, ServletException { 20 request.setCharacterEncoding(encoding); 21 response.setCharacterEncoding(encoding); 22 chain.doFilter(request, response);//放行 23 } 24 25 public void destroy() {//过滤器被销毁(服务器停止,才会被销毁) 26 27 } 28 }
web.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <!-- 编码过滤 --> 9 <filter> 10 <!-- 类名 --> 11 <filter-name>CharsetEncoding</filter-name> 12 <!-- 类路径 --> 13 <filter-class>com.shore.a_filter.CharsetEncoding</filter-class> 14 <init-param> 15 <!-- 参数名 --> 16 <param-name>encoding</param-name> 17 <!-- 编码 --> 18 <param-value>UTF-8</param-value> 19 </init-param> 20 </filter> 21 22 <filter-mapping> 23 <!-- 类名 --> 24 <filter-name>CharsetEncoding</filter-name> 25 <!-- /* 表示过滤(拦截)所有页面(请求) *.xxx 表示拦截该指定后缀结尾的页面(请求) --> 26 <url-pattern>/*</url-pattern> 27 </filter-mapping> 28 29 30 <welcome-file-list> 31 <welcome-file>index.jsp</welcome-file> 32 </welcome-file-list> 33 </web-app>
附录
过滤器
1 package com.shore.b_filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 public class LoginFilter implements Filter{ 15 private String[] passlist; 16 public void init(FilterConfig Config) throws ServletException { 17 passlist=Config.getInitParameter("passList").split(","); 18 System.out.println("第一步"); 19 } 20 21 public void doFilter(ServletRequest req, ServletResponse resp, 22 FilterChain chain) throws IOException, ServletException { 23 //会重复执行一次 24 System.out.println("第二步"); 25 //获取request对象 26 HttpServletRequest request=(HttpServletRequest)req; 27 HttpServletResponse response=(HttpServletResponse)resp; 28 //获取客户端的资源路径 29 String uri = request.getRequestURI(); 30 //获取请求资源路径 31 int index = uri.lastIndexOf("/");//获取路径中的最后一个斜杠的下标 32 String source = uri.substring(index+1);//截取斜杠后面的所有字符串 33 System.out.println(source); 34 boolean flag = false;//定义标记 35 for(String s:passlist){//遍历 36 if(s.trim().equals(source)){ 37 flag = true; 38 } 39 } 40 if(flag){//如果flag=true则放行 41 chain.doFilter(request, response); 42 }else{ 43 //获取session的内容 44 Object obj = request.getSession().getAttribute("user"); 45 if(obj == null){ 46 //未登录 47 request.setAttribute("msg","请先登录"); 48 request.getRequestDispatcher("index.jsp").forward(request, response); 49 }else{ 50 System.out.println("第五步"); 51 chain.doFilter(request, response);//放行 52 } 53 } 54 } 55 56 public void destroy() { 57 58 } 59 }
web.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <!-- 配置过滤器--> 9 <filter> 10 <!-- 类名 --> 11 <filter-name>LoginFilter</filter-name> 12 <!-- 类路径 --> 13 <filter-class>com.shore.b_filter.LoginFilter</filter-class> 14 <init-param> 15 <!-- 参数名 --> 16 <param-name>passList</param-name> 17 <!-- passList参数的值(放行index和reg页面,即:不用登陆也可直接访问这两个页面) --> 18 <param-value>index.jsp,reg.jsp</param-value> 19 </init-param> 20 </filter> 21 <filter-mapping> 22 <!-- 类名 --> 23 <filter-name>LoginFilter</filter-name> 24 <!-- 拦截所有jsp页面 --> 25 <url-pattern>*.jsp</url-pattern> 26 </filter-mapping> 27 28 <!-- 编码过滤 --> 29 <filter> 30 <filter-name>CharsetEncoding</filter-name> 31 <filter-class>com.shore.a_filter.CharsetEncoding</filter-class> 32 <init-param> 33 <param-name>encoding</param-name> 34 <param-value>UTF-8</param-value> 35 </init-param> 36 </filter> 37 <filter-mapping> 38 <filter-name>CharsetEncoding</filter-name> 39 <!-- /* 表示过滤(拦截)所有页面(请求) *.xxx 表示拦截该指定后缀结尾的页面(请求) --> 40 <url-pattern>/*</url-pattern> 41 </filter-mapping> 42 43 44 <servlet> 45 <description>This is the description of my J2EE component</description> 46 <display-name>This is the display name of my J2EE component</display-name> 47 <servlet-name>LoginServlet</servlet-name> 48 <servlet-class>com.shore.b_servlets.LoginServlet</servlet-class> 49 </servlet> 50 <servlet> 51 <description>This is the description of my J2EE component</description> 52 <display-name>This is the display name of my J2EE component</display-name> 53 <servlet-name>RegisterServlet</servlet-name> 54 <servlet-class>com.shore.b_servlets.RegisterServlet</servlet-class> 55 </servlet> 56 57 <servlet-mapping> 58 <servlet-name>LoginServlet</servlet-name> 59 <url-pattern>/LoginServlet</url-pattern> 60 </servlet-mapping> 61 <servlet-mapping> 62 <servlet-name>RegisterServlet</servlet-name> 63 <url-pattern>/RegisterServlet</url-pattern> 64 </servlet-mapping> 65 66 67 <welcome-file-list> 68 <welcome-file>index.jsp</welcome-file> 69 </welcome-file-list> 70 </web-app>
servlet
1 package com.shore.b_servlets; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class LoginServlet extends HttpServlet { 12 13 public void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 doPost(request, response); 16 } 17 18 public void doPost(HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException { 20 String name=request.getParameter("uname"); 21 String pass=request.getParameter("upass"); 22 System.out.println("第四步"); 23 if(!"".equals(name.trim())){ 24 //将当前登录的用户存储到session中 25 request.getSession().setAttribute("user",name); 26 //跳转到主页 27 response.sendRedirect("main.jsp"); 28 29 }else{ 30 request.setAttribute("msg","请输入账号和密码"); 31 request.getRequestDispatcher("index.jsp").forward(request, response); 32 } 33 } 34 }
登录页面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 <body> 23 <% 24 System.out.println("第三步"); 25 %> 26 <form action="LoginServlet1" method="post"> 27 <input type="text" name="uname"/> 28 <input type="password" name="upass"/> 29 <input type="submit" value="登录"/> 30 <span>没有账号?<a href="reg.jsp">立即注册</a></span> 31 </form> 32 <hr> 33 <span style="color: red;">${requestScope.msg}</span> 34 </body> 35 </html>
注册页面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'reg.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <form action="RegisterServlet"> 27 <input type="text" name="uname" placeholder="请输入账号"/> 28 <input type="password" name="upass" placeholder="请输入密码"/> 29 <input type="password" name="upass" placeholder="请输入重复密码"/> 30 <input type="submit" value="注册"/> 31 </form> 32 </body> 33 </html>
主页面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'main.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 欢迎来到主页 27 </body> 28 </html>
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/10703859.html 欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |