栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在哪里可以找到将正则表达式应用于输出的Java Servlet过滤器?[关闭]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在哪里可以找到将正则表达式应用于输出的Java Servlet过滤器?[关闭]

我找不到一个,所以写了一个:

RegexFilter.java

package com.example;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.regex.Pattern;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.HttpServletResponse;public final class RegexFilter implements Filter {    private List<Pattern> searchPatterns;    private List<String> replaceStrings;        public void init(FilterConfig filterConfig) {        Map<String, String> patternMap = new HashMap<String, String>();        // Walk through the parameters to find those whose names start with        // search        Enumeration<String> names = (Enumeration<String>) filterConfig.getInitParameterNames();        while (names.hasMoreElements()) { String name = names.nextElement(); if (name.startsWith("search")) {     patternMap.put(name.substring(6), filterConfig.getInitParameter(name)); }        }        this.searchPatterns = new ArrayList<Pattern>(patternMap.size());        this.replaceStrings = new ArrayList<String>(patternMap.size());        // Walk through the parameters again to find the matching replace params        names = (Enumeration<String>) filterConfig.getInitParameterNames();        while (names.hasMoreElements()) { String name = names.nextElement(); if (name.startsWith("replace")) {     String searchString = patternMap.get(name.substring(7));     if (searchString != null) {         this.searchPatterns.add(Pattern.compile(searchString));         this.replaceStrings.add(filterConfig.getInitParameter(name));     } }        }    }    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {        // Wrap the response in a wrapper so we can get at the text after calling the next filter        PrintWriter out = response.getWriter();        CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);        chain.doFilter(request, wrapper);        // Extract the text from the completed servlet and apply the regexes        String modifiedHtml = wrapper.toString();        for (int i = 0; i < this.searchPatterns.size(); i++) { modifiedHtml = this.searchPatterns.get(i).matcher(modifiedHtml).replaceAll(this.replaceStrings.get(i));        }        // Write our modified text to the real response        response.setContentLength(modifiedHtml.getBytes().length);        out.write(modifiedHtml);        out.close();    }    public void destroy() {        this.searchPatterns = null;        this.replaceStrings = null;    }}

CharResponseWrapper.java

package com.example;import java.io.CharArrayWriter;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpServletResponseWrapper;public class CharResponseWrapper extends HttpServletResponseWrapper {    private CharArrayWriter output;    public CharResponseWrapper(HttpServletResponse response) {        super(response);        this.output = new CharArrayWriter();    }    public String toString() {        return output.toString();    }    public PrintWriter getWriter() {        return new PrintWriter(output);    }}

示例web.xml

<web-app>    <filter>      <filter-name>RegexFilter</filter-name>      <filter-class>com.example.RegexFilter</filter-class>      <init-param><param-name>search1</param-name><param-value><![CDATA[(<s*as[^>]*)(?<=s)targets*=s*(?:'_parent'|"_parent"|_parent|'_top'|"_top"|_top)]]></param-value></init-param>      <init-param><param-name>replace1</param-name><param-value>$1</param-value></init-param>    </filter>    <filter-mapping>      <filter-name>RegexFilter</filter-name>      <url-pattern>/*</url-pattern>    </filter-mapping></web-app>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号