我从这里下载了 UpperCaseFilter 和 CharResponseWrapper 类:
大写过滤器.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 基金会网站下载了“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,因此无法向文本写入任何内容)。
我无法弄清楚为什么 UpperCaseFilter.getWriter() 永远不会被调用。