拦截器概念

在HttpServletRequest 到达Servlet 之前,拦截客户的HttpServletRequest 。
根据需要检查HttpServletRequest ,也可以修改HttpServletRequest 头和数据。

编写拦截器

  1. package com.filter;
  2. import java.io.IOException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. /**
  14. * @author 19745
  15. */
  16. public class FirstFilter implements Filter {
  17. @Override
  18. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  19. HttpServletRequest request = (HttpServletRequest) servletRequest;
  20. HttpServletResponse response = (HttpServletResponse) servletResponse;
  21. request.setCharacterEncoding("UTF-8");
  22. // String ip = request.getRemoteAddr();
  23. // String url = request.getRequestURL().toString();
  24. // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  25. // Date d = new Date();
  26. // String date = sdf.format(d);
  27. //
  28. // System.out.printf("%s %s 访问了 %s%n", date, ip, url);
  29. filterChain.doFilter(request, response);
  30. }
  31. @Override
  32. public void init(FilterConfig arg0) throws ServletException {
  33. }
  34. }

配置web.xml

  1. <filter>
  2. <filter-name>FirstFilter</filter-name>
  3. <filter-class>com.filter.FirstFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>FirstFilter</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

url-pattern内的/* 代表拦截所有资源

中文处理

通过Servlet获取中文参数 的章节中知道,可以通过

request.setCharacterEncoding(“UTF-8”);

正确获取UTF-8编码的中文。但是如果有很多servlet都需要获取中文,那么就必须在每个Servlet中增加这段代码。
有一个简便的办法,那就是通过Filter过滤器进行中文处理 ,那么所有的Servlet都不需要单独处理了。

  1. HttpServletRequest request = (HttpServletRequest) servletRequest;
  2. HttpServletResponse response = (HttpServletResponse) servletResponse;
  3. request.setCharacterEncoding("UTF-8");

注意,需要重启tomcat服务。

在拦截器中添加request中文处理

image.png

取消拦截器和servlert中的request的中文处理

image.png

登录验证

如果session中没有保存username,强制跳转回登录界面。

  1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
  2. throws IOException, ServletException {
  3. HttpServletRequest request = (HttpServletRequest) req;
  4. HttpServletResponse response = (HttpServletResponse) res;
  5. String uri = request.getRequestURI();
  6. System.out.println("uri=" + uri);
  7. if (uri.endsWith("login.html") || uri.endsWith("login")) {
  8. chain.doFilter(request, response);
  9. return;
  10. }
  11. String userName = (String) request.getSession().getAttribute("userName");
  12. if (null == userName) {
  13. response.sendRedirect("login.html");
  14. return;
  15. }
  16. chain.doFilter(request, response);
  17. }