javaWeb 使用 filter 处理 html 标签问题

Posted MarcoReus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaWeb 使用 filter 处理 html 标签问题相关的知识,希望对你有一定的参考价值。

1.web.xml代码

    <filter>
        <filter-name>htmlFilter</filter-name>
        <filter-class>de.bvb.web.filter.HtmlFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HtmlFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping> 

2.de.bvb.web.filter.HtmlFilter.java 代码

package de.bvb.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class HtmlFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        chain.doFilter(new HtmlHttpServletRequestWrapper(request), response);
    }

    class HtmlHttpServletRequestWrapper extends HttpServletRequestWrapper {

        private HttpServletRequest request;

        public HtmlHttpServletRequestWrapper(HttpServletRequest request) {
            super(request);
            this.request = request;
        }

        @Override
        public String getParameter(String name) {
            return filter(request.getParameter(name));
        }

        public String filter(String message) {
            if (message == null)
                return (null);
            char content[] = new char[message.length()];
            message.getChars(0, message.length(), content, 0);
            StringBuffer result = new StringBuffer(content.length + 50);
            for (int i = 0; i < content.length; i++) {
                switch (content[i]) {
                case ‘<‘:
                    result.append("&lt;");
                    break;
                case ‘>‘:
                    result.append("&gt;");
                    break;
                case ‘&‘:
                    result.append("&amp;");
                    break;
                case ‘"‘:
                    result.append("&quot;");
                    break;
                default:
                    result.append(content[i]);
                }
            }
            return (result.toString());
        }

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }

}

 

以上是关于javaWeb 使用 filter 处理 html 标签问题的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb的过滤器Filter

Filter过滤器-JavaWeb三大组件之一

javaweb学习总结(四十三)——Filter高级开发

JavaWeb三大组件之Filter

JavaWeb 之 Filter:过滤器

过滤器与监听器 ----javaweb-10