查看Servlet的API发现问题:
public java.io.PrintWriter getWriter() throws java.io.IOException
-
Returns a
PrintWriter
object that can send character text to the client. ThePrintWriter
uses the character encoding returned bygetCharacterEncoding()
. If the response's character encoding has not been specified as described ingetCharacterEncoding
(i.e., the method just returns the default valueISO-8859-1
),getWriter
updates it toISO-8859-1
.Calling flush() on the
PrintWriter
commits the response.Either this method or
getOutputStream()
may be called to write the body, not both. -
-
Returns:
-
a
PrintWriter
object that can return character data to the client
Throws:
-
UnsupportedEncodingException
- if the character encoding returned bygetCharacterEncoding
cannot be used -
java.lang.IllegalStateException
- if thegetOutputStream
method has already been called for this response object -
java.io.IOException
- if an input or output exception occurred
See Also:
-
getOutputStream()
,setCharacterEncoding(java.lang.String)
-
a
解决方案:
加入如下代码:
out.clear();
out = pageContext.pushBody();
代码作用:
out.clear():清空缓存的内容。
pageContext.pushBody():参考API
public BodyContent pushBody()
-
Return a new BodyContent object, save the current "out" JspWriter, and update the value of the "out" attribute in the page scope attribute namespace of the PageContext.
-
Returns:
- the new BodyContent
·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容)
·保存JspWriter实例的对象out
·更新PageContext的out属性的内容