性能和过滤器

时间:2013-09-26 09:25:47

标签: java web

过滤器是否有任何原因导致服务器响应变慢?

我有一个带有以下过滤器的网络应用程序:

package com.bingo.filters;

import java.io.IOException;
import java.util.Arrays;
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.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class CacheFilter implements Filter {

    static class CacheControlHeaderResponse extends HttpServletResponseWrapper {

        public static final String[] CACHEABLE_CONTENT_TYPES = new String[] {
            "text/css", "text/javascript", "image/png", "image/jpeg",
            "image/gif", "image/jpg" };

        static {
            Arrays.sort(CACHEABLE_CONTENT_TYPES);
        }

        public CacheControlHeaderResponse(HttpServletResponse response) {
            super(response);
        }

        @Override
        public void setContentType(String contentType) {

                super.setHeader("Expires", "-1");
                super.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
                super.setHeader("Pragma", "no-cache");

            super.setContentType(contentType);
        }
    }

    public CacheFilter() {

    }

    public void destroy() {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        chain.doFilter(request, new CacheControlHeaderResponse(res));   
    }

    public void init(FilterConfig fConfig) throws ServletException {

    }
}

这是我添加到web.xml的配置

<filter>
    <filter-name>cFilter</filter-name>
    <filter-class>com.bingo.filters.CacheFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cFilter</filter-name>
    <url-pattern>/bingo/*</url-pattern>
</filter-mapping>

当我使用这个新代码部署战争时,应用程序服务器的响应速度非常慢。检索页面需要6或7秒。

但如果我在没有上述代码的情况下部署旧的war文件,那么响应非常快。

上述代码有什么问题可以解释性能问题吗?

0 个答案:

没有答案