-
Filter:过滤器,用来过滤网站的数据(处理中文乱码,登录验证…)
-
Filter开发步骤
- 导包配置maven
- 编写过滤器(导包:javax.servlet.Filter)
public class CharacterEncodingFilter implements Filter { //初始化 public void init(FilterConfig filterConfig) throws ServletException { System.out.println("CharacterEncodingFilter已经初始化"); } //chain:链 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); System.out.println("CharacterEncodingFilter执行前"); chain.doFilter(request,response); //让请求继续走,如果不写程序到这里自动停止 System.out.println("CharacterEncodingFilter执行后"); } //销毁:web服务器关闭时,过滤器会被销毁 public void destroy() { System.out.println("CharacterEncodingFilter已经销毁"); } }
- web.xml配置过滤器
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.java.filter.CharacterEncodingFilter</filter-class> </filter> <!--过滤/servlet路径下的所有请求--> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/servlet/*</url-pattern> </filter-mapping>
-
总结:
- 过滤器中所有的代码,在过滤特定请求的时候都会执行
- 必须要让过滤器继续通行
- chain.doFilter(request,response);必须写
案例:用户登录之后才能进入主页,用户注销之后则不能进入
- 分析
- 1.用户登录之后向Session中放入用户数据信息
- 2.进入主页的时候要判断用户是否已经登录(过滤器)
登录
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取前端请求的参数
String username = req.getParameter("username");
if (username.equals("admin")){ //登录成功
req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId());
resp.sendRedirect("/sys/success.jsp");
}else { //登录失败
resp.sendRedirect("/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
登出
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String user_session = (String) req.getSession().getAttribute(Constant.USER_SESSION);
if (user_session != null){
req.getSession().removeAttribute(Constant.USER_SESSION);
resp.sendRedirect("/login.jsp");
}else {
resp.sendRedirect("/login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
过滤器
public class SysFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (request.getSession().getAttribute(Constant.USER_SESSION)==null) {
response.sendRedirect("/error.jsp");
}
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {
}
}
提取常量工具类
public class Constant {
public final static String USER_SESSION = "USER_SESSION";
}