java.lang.IllegalStateException: STREAM
at org.eclipse.jetty.server.Response.getWriter(Response.java:707)
java.lang.IllegalStateException: WRITER
at org.eclipse.jetty.server.Response.getOutputStream(Response.java:681)
今天在写一个框架的时候遇到一个问题,记录一下,报错如上,查看javax.servlet.ServletResponse接口中的定义如下:
/**
* Returns a {@link ServletOutputStream} suitable for writing binary
* data in the response. The servlet container does not encode the
* binary data.
* <p> Calling flush() on the ServletOutputStream commits the response.
* Either this method or {@link #getWriter} may
* be called to write the body, not both.
*
* @return a {@link ServletOutputStream} for writing binary data
*
* @exception IllegalStateException if the <code>getWriter</code> method
* has been called on this response
*
* @exception IOException if an input or output exception occurred
*
* @see #getWriter
*
*/
public ServletOutputStream getOutputStream() throws IOException;
/**
* Returns a <code>PrintWriter</code> object that
* can send character text to the client.
* The <code>PrintWriter</code> uses the character
* encoding returned by {@link #getCharacterEncoding}.
* If the response's character encoding has not been
* specified as described in <code>getCharacterEncoding</code>
* (i.e., the method just returns the default value
* <code>ISO-8859-1</code>), <code>getWriter</code>
* updates it to <code>ISO-8859-1</code>.
* <p>Calling flush() on the <code>PrintWriter</code>
* commits the response.
* <p>Either this method or {@link #getOutputStream} may be called
* to write the body, not both.
*
*
* @return a <code>PrintWriter</code> object that
* can return character data to the client
*
* @exception UnsupportedEncodingException
* if the character encoding returned
* by <code>getCharacterEncoding</code> cannot be used
*
* @exception IllegalStateException
* if the <code>getOutputStream</code>
* method has already been called for this
* response object
*
* @exception IOException
* if an input or output exception occurred
*
* @see #getOutputStream
* @see #setCharacterEncoding
*
*/
public PrintWriter getWriter() throws IOException;
注意这两句:
Either this method or {@link #getWriter} may be called to write the body, not both.
Either this method or {@link #getOutputStream} may be called to write the body, not both.
再看jetty的源码,jetty.server.Response的源码:
public ServletOutputStream getOutputStream() throws IOException
{
if (_outputState!=NONE && _outputState!=STREAM)
throw new IllegalStateException("WRITER");
ServletOutputStream out = _connection.getOutputStream();
_outputState=STREAM;
return out;
}
public PrintWriter getWriter() throws IOException
{
if (_outputState!=NONE && _outputState!=WRITER)
throw new IllegalStateException("STREAM");
/* if there is no writer yet */
if (_writer==null)
{
/* get encoding from Content-Type header */
String encoding = _characterEncoding;
if (encoding==null)
{
/* implementation of educated defaults */
if(_cachedMimeType != null)
encoding = MimeTypes.getCharsetFromContentType(_cachedMimeType);
if (encoding==null)
encoding = StringUtil.__ISO_8859_1;
setCharacterEncoding(encoding);
}
/* construct Writer using correct encoding */
_writer = _connection.getPrintWriter(encoding);
}
_outputState=WRITER;
return _writer;
}
从源码的第3-4行可以得以验证,也就是说对于同一个response实例你只能使用其中的一个方法,不能两者混用。
分享到:
相关推荐
标题 "java.lang.IllegalStateException: OutputStream already obtain" 涉及到的是Java编程中的一个常见错误,特别是当处理I/O流时。这个异常通常在尝试获取已经存在的OutputStream实例时抛出,表明该输出流已经被...
NULL 博文链接:https://albert0707.iteye.com/blog/562969
然而,对于初学者来说,遇到错误是常有的事,比如“java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource”。这个异常表明在运行时,系统无法找到指定的类,即Apache Commons DBCP的...
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/ConditionalTagSupport 问题-附件资源
javax.servlet.ServletResponse.class javax.servlet.ServletConfig.class javax.servlet.ServletInputStream.class javax.servlet.ServletOutputStream.class javax.servlet.FilterChain.class javax.servlet....
Servlet-api.jar是Java Web开发中不可或缺的一个库文件,它包含了Servlet和JSP(JavaServer Pages)规范的API接口。这个库文件主要用于与Web服务器交互,处理HTTP请求,并生成响应。在本文中,我们将深入探讨Servlet...
【描述】:servlet-api.jar 是一个重要的Java Servlet API库,它包含了处理Web请求的核心接口和类。这个库主要用于开发基于Java的Web应用程序,特别是Servlets。同时,描述中提到了javax.zip,这通常指的是Java的ZIP...
在Java Web开发中,`javax.servlet.jar`和`javax.servlet.jsp.jar`是两个非常重要的库文件,它们分别提供了Servlet和JSP(JavaServer Pages)的核心API。这两个库是开发基于Java的Web应用程序不可或缺的部分,特别是...
1. **Servlet接口**:Servlet是Java Web应用的核心,它是一个实现了 javax.servlet.Servlet 接口的类,负责处理客户端请求并生成响应。例如,GenericServlet 和 HttpServlet 是Servlet接口的常用抽象基类,提供了...
javax.servlet.jsp.jstl-api-1.2.1.jar
servlet-api.jar 适用于import javax.servlet.http.HttpSession;异常 直接下载后直接导入 即可,
例如,`javax.servlet.jsp.PageContext`提供了对请求、响应、session以及应用程序范围内的对象的访问,而`javax.servlet.jsp.JspWriter`则用于输出内容到响应流。 **Servlet API** Servlet是Java编写的一种服务器...
Java.Web整合开发王者归来..JSP.Servlet%..z07
Java.Web整合开发王者归来..JSP.Servlet%..z01
Java.Web整合开发王者归来..JSP.Servlet%..zip 也是得全部下载在解压才好用
Servlet-API.jar和javax.servlet.jar是Java Web开发中两个至关重要的库文件,它们包含了Servlet和相关API的实现。在深入理解这两个文件之前,我们先要了解Servlet的概念。 Servlet是一种Java类,它扩展了服务器的...
Java.Web整合开发王者归来..JSP.Servlet%..z04
( Java.Web整合开发王者归来..JSP.Servlet%..z05