查看Servlet的API发现问题:
public java.io.PrintWriter getWriter()
throws java.io.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.
Returns:
a PrintWriter
object that can return character data to the client
Throws:
UnsupportedEncodingException
- if the character encoding returned by getCharacterEncoding
cannot be used
java.lang.IllegalStateException
- if the getOutputStream
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)
如API所言,由于ServletResponse.getOutputStream()方法和该方法都有可能被调用,来输出JSP页面的内容,如果其中的一个方法被调用了,再调用另一个方法就会抛出异常。
解决方案:
加入如下代码:
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属性的内容
分享到:
相关推荐
NULL 博文链接:https://javal.iteye.com/blog/1993903
1.在tomcat6.0下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法 在tomcat6.0下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有...
纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料 出这个错误一般就是下面2个.....
验证码出现getOutputStream() has already been called for this response错误解决
解决了getOutputStream() has already been called for this response. 并将产生验证码的逻辑从JSP页面中分离出来,单独写了一个类 便于重用。
NULL 博文链接:https://nethub2.iteye.com/blog/2023937
servlet的response可以对返回的内容和格式进行一些设置。
Returns a boolean indicating whether the named response header has already been set. contextDestroyed(ServletContextEvent) - Method in interface javax.servlet.ServletContextListener Notification ...
* This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...
OutputStream o = response.getOutputStream(); // 将图片转换成字符串 File f = new File("f:\\Vista.png"); FileInputStream fis = new FileInputStream( f ); byte[] bytes = new byte[fis.available...
heads是excel表头,datalist是数据public void createExcelStream(HttpServletResponse response,String filename,String[] heads,List[]> datalist){ try{ OutputStream os = response.getOutputStream();...
IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); System.out.println("输出成功!"); } HTML前端接response收流: <source src="http://localhost:8081/file/output...
随机获取验证码 public class CheckNumberServlet extends HttpServlet { ... ServletOutputStream sos = response.getOutputStream();…………………………………………………………………………………………
public void code(HttpServletRequest request, HttpServletResponse response) throws IOException{ String[] tableNames = new String[]{};... IOUtils.write(data, response.getOutputStream()); }
OutputStream outs = response.getOutputStream();// 获取文件输出IO流 BufferedOutputStream bouts = new BufferedOutputStream(outs); response.setContentType("application/x-download");// 设置response...
ServletOutputStream responseOutputStream = response.getOutputStream(); ImageIO.write(bufferImage, "JPEG", responseOutputStream); responseOutputStream.flush(); responseOutputStream.close(); } } ...
import java.io.*; import java.util.*; import javax.servlet.*; ... public class MyServlet ... OutputStream out_Stream = response.getOutputStream(); out_Stream.write( data ); out_Stream.close(); } }
ServletOutputStream servletOutputStream = response.getOutputStream(); byte[] b = new byte[1024]; int i = 0; FileInputStream fis = new java.io.FileInputStream(realName); while ((i = fis.read(b)) > 0)...
用与java web 开发,java 实现的数字验证码的生成 class Image{} *" %> <jsp:useBean id="image" scope="session" class="myclass.test....ImageIO.write(image.creatImage(), "JPEG", response.getOutputStream()); %>