拦截器也叫过滤器,拦截器就是前端和servlet之间的一个东西,可以用拦截器进行编码统一和拦截没登陆就进页面的
实现Filter(Servlet包下的)
那三个方法是init、doFilter、destroy,它们是生命周期
init是初始化,doFilter是内容,destroy是销毁
拦截没登陆的
1.置web.xml方法
这里的配置替代的是java文件中的注解(java文件也要有只是可以不写注解),个人认为还是注解更方便
<!-- 配置过滤器 -->
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.hz.filter.LoginFilter</filter-class>
</filter>
<!-- 配置过滤器映射 -->
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/servlet/*</url-pattern> <!-- 只拦截 /secure 路径下的请求 -->
</filter-mapping>
2.使用注解配置:
package com.hz.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/servlet/*")
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Filter 初始化,可以不做任何事
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
//转换类型,创建session对
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
HttpSession session = httpServletRequest.getSession();
if (session.getAttribute("smbmsProvider")==null){
// 如果没有登录,返回登录页
httpServletResponse.sendRedirect("/login.jsp");
}else {
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
统一设置编码:
1、使用注解配置:
@WebFilter("/*")
package com.hz.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
// 设置请求的字符编码
httpServletRequest.setCharacterEncoding("UTF-8");
// 设置响应的字符编码
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("text/html;charset=UTF-8");
// 继续执行过滤链
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
2.使用web.xml配置文件
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.hz.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern> <!-- 拦截所有请求 -->
</filter-mapping>
标签:拦截器,public,Filter,init,过滤器,import,servlet,javax
From: https://blog.csdn.net/qq_62859013/article/details/143713153