基础9——过滤器
Posted 鹏达君
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基础9——过滤器相关的知识,希望对你有一定的参考价值。
1.说明:对过滤器的理解我们不要从请求作为起点,而是项目本身的资源作为过滤的起点,然后根据其要过滤的项目资源(url),在根据他的模式,就可以知道它要过滤的是啥资源了,然后看它的方法,就可以知道它过滤是处理些什么逻辑,当然,最后还需要把该请求传下去,如果是后面还有过滤器就继续过滤,如果没有,就到了servlet或者jsp了,而过滤的顺序与其注册的顺序是一致的。
2.创建一个过滤器
a.步骤:
1)建一个过滤器,根据需求写好逻辑方法
2)进行注册(xml或者直接注释进行注册)
b.代码
package com.guolvqi; 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.annotation.WebFilter; /** * Servlet Filter implementation class fristFilter */
//代表过滤映射为/downloadServlet的项目资源 @WebFilter("/downloadServlet") public class fristFilter implements Filter { /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("过滤器实施了"); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
3.Filter的生命周期
4.过滤器的api
5.过滤器的配置
5.案例:使用Filter进行字符编码转换
1)servlet
package com.anli; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestFilter */ @WebServlet("/TestFilter") public class TestFilter extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("其内容为通过了过滤器设置编码才能正常显示的"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
2)Filter
package com.guolvqi; 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.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; /** * Servlet Filter implementation class fristFilter */ @WebFilter(urlPatterns= {"/*"}, initParams= { @WebInitParam(name="encoding",value="utf-8"),@WebInitParam(name="encoding1",value="utf-8")}) public class fristFilter implements Filter { String enconding; /** * @see Filter#destroy() */ public void destroy() { enconding=null; } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if(enconding!=null) { //这边的设置既可以是输入的时候输入中文,也可以是输出的时候是中文 request.setCharacterEncoding(enconding); response.setCharacterEncoding(enconding); } chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { enconding=fConfig.getInitParameter("encoding"); } }
3)测试:
以上是关于基础9——过滤器的主要内容,如果未能解决你的问题,请参考以下文章
;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk