几种典型的过滤器应用

1.使浏览器不缓存页面的过滤器:

有 3 个 HTTP 响应头字段都可以禁止浏览器缓存当前页面,它们在 Servlet 中的示例代码如下:

response.setDateHeader("Expires",-1);

response.setHeader("Cache-Control","no-cache"); 

response.setHeader("Pragma","no-cache"); 

并不是所有的浏览器都能完全支持上面的三个响应头,因此最好是同时使用上面的三个响应头

a.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>    
    <a href="b.html">TO BBB PAGE</a>
    <br><br>
    <img alt="" src="Hydrangeas.jpg">
</body>
</html>

b.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>    
    <a href="a.html">TO AAA PAGE</a>
    <br><br>
</body>
</html>

NoCacheFilter

扫描二维码关注公众号,回复: 11153819 查看本文章
package com.aff.filter.cache;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aff.filter.HttpFilter;

public class NoCacheFilter extends HttpFilter {

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        response.setDateHeader("Expires",-1);
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Pragma","no-cache");

        chain.doFilter(request, response);
        
    }
}

目录

2.字符编码的过滤器

通过配置参数encoding指明使用何种字符编码,以处理Html Form请求参数的中文问题

    编写一个 EncodingFilter
   ①. 读取 web.xml 文件中配置的当前 WEB 应用的初始化参数 encoding
   ②. 指定请求的字符编码为 1 读取到的编码
   ③. 调用 chain.doFilter() 方法 "放行" 请求
         request.setCharacterEncoding("UTF-8");

a.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="b.jsp" method="post">
        name: <input type="text" name="name"/>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>

b.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <%--
        //编写一个 EncodingFilter
        //1. 读取 web.xml 文件中配置的当前 WEB 应用的初始化参数 encoding
        //2. 指定请求的字符编码为 1 读取到的编码
        //3. 调用 chain.doFilter() 方法 "放行" 请求
        request.setCharacterEncoding("UTF-8");
    --%>
    Hello: ${param.name }
</body>
</html>

EncodingFilter

package com.aff.filter.encoding;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aff.filter.HttpFilter;

public class EncodingFilter extends HttpFilter {

    private String encoding;
    @Override
    protected void init() {
        encoding = getFilterConfig().getServletContext().getInitParameter("encoding");
    }

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <context-param>
        <param-name>password</param-name>
        <param-value>123456</param-value>
    </context-param>

    <context-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </context-param>

    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>com.aff.filter.encoding.EncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/encoding/*</url-pattern>
    </filter-mapping>

</web-app>

目录

3.检测用户是否登陆的过滤器:

情景:系统中的某些页面只有在正常登陆后才可以使用,用户请求这些页面时要检查 session 中有无该用户信息,

           但在所有必要的页面加上session的判断相当麻烦的事情

解决方案:编写一个用于检测用户是否登陆的过滤器,如果用户未登录,则重定向到指的登录页面

要求:需检查的在 Session 中保存的关键字;

           如果用户未登录,需重定向到指定的页面(URL不包括 ContextPath);

           不做检查的URL列表(以分号分开,并且 URL 中不包括 ContextPath)都要采取可配置的方式

猜你喜欢

转载自www.cnblogs.com/afangfang/p/12806307.html