课程信息
● 学习课程:Java工程师2022版
● 章节名称:过滤器-Fileter过滤器应用实践
● 讲师:八戒猪
课程内容
WebFilter注解的使用
配置形式维护性更好,适合应用全局过滤
注解形式开发体验更好,适合小型项目敏捷开发
过滤器更推荐使用配置方式,servlet推荐使用注解方式。
1、过滤器的配置形式:
<filter>
<filter-name>firstfilter</filter-name>
<filter-class>indi.xxmas.filter.FirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>firstfilter</filter-name>
<!-- /*对所有的请求进行拦截 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
2、过滤器的注解形式:
@WebFilter(filterName="annotationFileter",urlPatterns="/*")
public class AnnotationFileter implements Filter{}
开发字符集过滤器
Web中文乱码的解决
GET请求 -server.xml中增加URIEncoding="UTF-8"
POST请求-使用request.setCharacterEncoding(“UTF_8”)
响应response.setContentType(“text/html;charset=UTF-8”)
public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain){
HttpServletRequest req = (HttpServletRequest)request;
HttpServeltResponse resp = (HttpSerbletResponse) response;
req.setCharaterEncoding("UTF-8");
resp.setContentType("text/html;charset="UTF-8");
chain.doFilter(request, response);
}
3.过滤器参数化
允许配置信息放在web.xml中设置过滤参数
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>com.imooc.filter.CharacterEncodingFilter</filter-class>
<!-- 初始化参数设置-->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>encoding1</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
public class CharacterEncodingFilter implements Filter {
//1、设置全局变量
private String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// 2、从配置文件中获取初始化参数
encoding = filterConfig.getInitParameter("encoding");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
// 针对post的请求进行字符集设置
request.setCharacterEncoding(encoding);//3、引入参数
response.setContentType("text/html;charset=" + encoding);
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
《注解形式的初始化参数设置》
过滤器不推荐使用注解方式进行配置。注解中,配置注解需要使用{}
@WebFilter(filterName = "characterEncoding", urlPatterns = "/*",
initParams = {
@WebInitParam(name = "encoding", value = "utf-8"),
@WebInitParam(name = "edcoding2", value = "GBK")
})//注解中的数组,需要使用{}
4.url-pattern常用写法
/index.html -执行资源精准配皮
/servlet/*以前缀进行模糊匹配
*.html以后缀进行模糊匹配
不支持前缀和后缀一起模糊匹配
5.过滤链的开发注意事项
每一个过滤器应具有单独的职能
过滤器的执行顺序以为准
调用chain.doFilter()请求向后传递
6.多端设备自动匹配
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws Exception {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String url = req.getRequestURI();
String userAgent = request.getHeader("user-agent");
String targetUri = null;
if(uri.startsWith("/desktop") || uri.startsWith("/mobile"){
chain.doFilter(servletRequest, servletResponse);
}else {
if(userAgent.toLowerCase().indexOf("android") != -1 || userAgent.toLowerCase().indexOf("iphone")){
targetUri = "/mobile" + uri;
}else {
targetUri = "/desktop" + uri;
}
resp.sendRedirect(targetUri);
}
}
监听器-Listener
过滤器Filter是主动对URL过滤拦截
监听器Listener是被动触发的对Web对象进行监听
三种监听对象
ServletContext–对全局ServletContext及其属性进行监听
HttpSession–对用户会话及其属性操作进行监听
ServletRequest–对请求及其属性操作进行监听
学习收获
学习了过滤器的配置形式,web.xml配置和注解配置形式,字符过滤器使用,
过滤器参数化,参数放在web.xml中,url-pattern设置过滤范围(精准,前缀,后缀),过滤链的开发注意事项,过滤器和监听器的区别
打卡截图
共同学习,写下你的评论
评论加载中...
作者其他优质文章