-
javaweb中过滤器Filter的解析与使用之解决中文乱码
1.什么是过滤器
过滤器实际上就是对web资源进行拦截,做些处理后再交给下一个过滤器或者servlet的一个程序
2.经典应用场景
(1)处理中文乱码
(2)实现权限拦截(登陆验证)
(3)过滤非法字符等
3.Filter开发步骤
(1)新建一个空的maven工程
(2)在pom.xml文件中导入相关依赖
1 <dependencies> 2 <dependency> 3 <groupId>javax.servlet</groupId> 4 <artifactId>servlet-api</artifactId> 5 <version>2.5</version> 6 </dependency> 7 <dependency> 8 <groupId>javax.servlet.jsp</groupId> 9 <artifactId>javax.servlet.jsp-api</artifactId> 10 <version>2.3.3</version> 11 </dependency> 12 <dependency> 13 <groupId>javax.servlet.jsp.jstl</groupId> 14 <artifactId>jstl-api</artifactId> 15 <version>1.2</version> 16 </dependency> 17 <dependency> 18 <groupId>taglibs</groupId> 19 <artifactId>standard</artifactId> 20 <version>1.1.2</version> 21 </dependency> 22 <dependency> 23 <groupId>mysql</groupId> 24 <artifactId>mysql-connector-java</artifactId> 25 <version>5.1.45</version> 26 </dependency> 27 </dependencies>
(3)在src/java目录下新建立一个com.xiaoma.filter.CharacterFilter的类文件,在类文件中实现Filter类并重写init、doFilter、destroy方法,参考代码如下:
其中chain参数总结:
①:过滤器中的所有代码,在过滤特定请求的时候都会执行
②:必须要让过滤器继续通行,说白了就是把这个请求往下转交,就需要用到下面的第19行大妈
1 package com.xiaoma.filter; 2 3 import javax.servlet.*; 4 import java.io.IOException; 5 6 public class CharacterFilter implements Filter { //web服务器启动的时候就开始初始化了,因为他要随时监听资源 7 @Override 8 public void init(FilterConfig filterConfig) throws ServletException { 9 System.out.println("CharacterFilter初始化中......"); 10 } 11 12 @Override 13 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 14 servletRequest.setCharacterEncoding("utf-8"); 15 servletResponse.setCharacterEncoding("utf-8"); 16 servletResponse.setContentType("text/html"); 17 18 System.out.println("CharacterFilter执行前......"); 19 filterChain.doFilter(servletRequest,servletResponse); 20 System.out.println("CharacterFilter执行后......"); 21 } 22 23 @Override 24 public void destroy() { 25 System.out.println("CharacterFilter销毁中......"); 26 } 27 }
(4)在src/java目录下新建立一个com.xiaoma.servlet.ShowServlet的类文件,随便输出点东西用于测试
1 package com.xiaoma.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 9 public class ShowServle extends HttpServlet { 10 @Override 11 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 12 13 resp.getWriter().write("你好,世界"); 14 } 15 16 @Override 17 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 18 super.doPost(req, resp); 19 } 20 }
(5)配置web.xml文件,包括servlet和filter的配置
1 <servlet> 2 <servlet-name>showServlet</servlet-name> 3 <servlet-class>com.xiaoma.servlet.ShowServle</servlet-class> 4 </servlet> 5 <!--使用了过滤器的mapping--> 6 <servlet-mapping> 7 <servlet-name>showServlet</servlet-name> 8 <url-pattern>/servlet/show</url-pattern> 9 </servlet-mapping> 10 <!--没有使用过滤器的mapping--> 11 <servlet-mapping> 12 <servlet-name>showServlet</servlet-name> 13 <url-pattern>/show</url-pattern> 14 </servlet-mapping> 15 16 <filter> 17 <filter-name>CharacterFilter</filter-name> 18 <filter-class>com.xiaoma.filter.CharacterFilter</filter-class> 19 </filter> 20 <filter-mapping> 21 <filter-name>CharacterFilter</filter-name> 22 <!--我们过滤servlet路径下的所有请求--> 23 <url-pattern>/servlet/*</url-pattern> 24 </filter-mapping>
(6)运行测试,访问http://localhost:8080/javaweb_filter_war_exploded/show的时候,也就是没用过滤器的时候依旧乱码,访问http://localhost:8080/javaweb_filter_war_exploded/servlet/show的时候也就是用了乱码过滤器后就不会乱码了,这样我们就不用每写一个servlet或者jsp都要进行编码的转换了
出处:https://www.cnblogs.com/XiaoMaGuai/p/15389822.html