为什么不在简单的servlet过滤器中调用getWriter()?

时间:2013-06-16 01:28:37

标签: java servlet-filters

我从here下载了UpperCaseFilter和CharResponseWrapper类:

UpperCaseFilter.java

package filters;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
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 UpperCaseFilter implements Filter {
  protected FilterConfig config;

  public void init(FilterConfig config) throws ServletException {
    this.config = config;
  }

  public void destroy() {
  }

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws ServletException, IOException {
    ServletResponse newResponse = response;
    System.out.println("UpperCasefilter begin");
    if (request instanceof HttpServletRequest) {
      newResponse = new CharResponseWrapper((HttpServletResponse) response);
    }

    chain.doFilter(request, newResponse);

    System.out.println("UpperCaseFilter end");
    if (newResponse instanceof CharResponseWrapper) {
      String text = newResponse.toString();
      if (text != null) {
        text = text.toUpperCase();
        response.getWriter().write(text);
      }
    }
  }
}

CharResponseWrapper.java

package filters;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

class CharResponseWrapper extends HttpServletResponseWrapper {
  protected CharArrayWriter charWriter;

  protected PrintWriter writer;

  protected boolean getOutputStreamCalled;

  protected boolean getWriterCalled;

  public CharResponseWrapper(HttpServletResponse response) {
    super(response);

    charWriter = new CharArrayWriter();
  }

  public ServletOutputStream getOutputStream() throws IOException {
    if (getWriterCalled) {
      throw new IllegalStateException("getWriter already called");
    }

    getOutputStreamCalled = true;
    return super.getOutputStream();
  }

  public PrintWriter getWriter() throws IOException {
    if (writer != null) {
      return writer;
    }
    if (getOutputStreamCalled) {
      throw new IllegalStateException("getOutputStream already called");
    }
    getWriterCalled = true;
    writer = new PrintWriter(charWriter);
    return writer;
  }

  public String toString() {
    String s = null;

    if (writer != null) {
      s = charWriter.toString();
    }
    return s;
  }
}

并将web.xml文件修改为:

 <filter>
    <filter-name>Filter1</filter-name>
    <filter-class>filters.UpperCaseFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

我还从Apache Foundation主网站下载了“apache-tomcat-6.0.37”。

当我运行Web应用程序时,我可以看到我的System.out.println()语句执行,所以我知道它正在运行UpperCaseFilter类。

但是,当我导航到http://127.0.0.1:8080时,没有一个字符是大写的!一切看起来都很正常(标准的Tomcat主页)。

我决定执行“./catalina.sh jpda start”以便调试它。我注意到CharWrapperResponse.getWriter()函数永远不会被调用,因此永远不会初始化“writer”对象。因此,当UpperCaseFilter调用CharResponseWrapper.toString()时,“text”变量最终为null,因为writer为null(即,因为writer = null,所以无法向text发送任何内容)。

我无法弄清楚为什么不会调用UpperCaseFilter.getWriter()。

0 个答案:

没有答案