我正在使用trimFilter(com.cj.trim.trimFilter)去除所有空行&在我的输出中创建的空格和UrlRewriteFilter来执行seo友好的URL。
许多页面工作正常,但有些页面抛出以下异常 java.lang.IllegalStateException:已为此响应调用了getWriter()
堆栈跟踪 [2015-02-19T00:06:49.718 + 0000] [glassfish 4.1] [警告] [] [javax.enterprise.web] [tid:_ThreadID = 49 _ThreadName = jk_listener(1)] [timeMillis:1424304409718] [levelValue: 900 [[] StandardWrapperValve [default]:Servlet的Servlet.service()默认引发异常 java.lang.IllegalStateException:已为此响应调用了getWriter() 在org.apache.catalina.connector.Response.getOutputStream(Response.java:746) 在org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:206) 在javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:142) 在javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:142) 在com.cj.trim.trimFilter.doFilter(未知来源) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 在org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176) at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145) at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92) at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:873) 在org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739) 在org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575) 在org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546) 在org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428) 在org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378) at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213) at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171) at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145) at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92) at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282) at com.sun.enterprise.v3.services.impl.ContainerMapper $ HttpHandlerCallable.call(ContainerMapper.java:459) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 在org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201) 在org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175) 在org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) 在org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) 在org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 在org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561) 在org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:565) 在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:545) 在java.lang.Thread.run(Thread.java:745)
我知道其中一个与其他人重叠。因此导致此问题。我需要知道如何解决这个问题。我应该在哪里更改代码来解决此问题。
答案 0 :(得分:1)
trimFilter& UrlRewriteFilter彼此冲突&这就是为什么它不在某些JSP中工作的原因。
我所做的只是删除了trimFilter&而是在web.xml中使用以下配置
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
现在问题解决了。
答案 1 :(得分:0)
我认为你试图将作者放在一个地方,然后再将输出流放在后面。 像response.getWriter();在一个地方 和response.getOutputStream();在另一个地方
你不能同时使用它们。
检查javadocs,他们已经提到过它。
http://docs.oracle.com/javaee/5/api/javax/servlet/ServletResponse.html#getWriter%28%29
PrintWriter getWriter() 抛出IOException
Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1. Calling flush() on the PrintWriter commits the response. Either this method or getOutputStream() may be called to write the body, not both.