有一个使用SerlvetResponse输出图像的例子,代码如下:
<%
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.fillRect(0,0,400,400);
g.setColor(new Color(255,0,0));
g.fillArc(20,20,100,100,30,120);
g.setColor(new Color(0,255,0));
g.fillArc(20,20,100,100,150,20);
g.setColor(new Color(0,0,255));
g.fillArc(20,20,100,100,270,120);
g.setColor(new Color(0,0,0));
g.drawString("red:climb" , 300, 80);
g.drawString("green:swim", 300, 120);
g.drawString("blue:jump", 300, 160);
ImageIO.write(image, "bmp", response.getOutputStream());
//out.clear();
//out = pageContext.pushBody();
%>
在Tomcat下运行时抛出如下异常:
at org.apache.catalina.connector.Response.getWriter(Response.java:601)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
..................
at org.apache.jsp.pages.drawImage_jsp._jspService(drawImage_jsp.java:84)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98).
...............
查看转换后的JSP代码,发现第84行如下(绿色高亮处):
finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
我们看到在JSP页面释放资源的时候,调用了ServetResponse.getWriter()方法,之后程序即抛出异常了,查看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页面的内容,如果其中的一个方法被调用了,再调用另一个方法就会抛出异常。
解决方法如下:
将JSP页面的最后两行代码的注释去掉,这两行代码的作用如下:
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
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 ...
this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { MsgBean bean = new MsgBean(); bean.setType("请求下线"); bean.setSource(ClientFrame...
BufferedWriter wrbuf = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream())); wrbuf.write("false\n"); wrbuf.flush(); }else{ Socket s2 = clients.get(str[0]); ...
servlet的response可以对返回的内容和格式进行一些设置。
outputS = new ObjectOutputStream(connection.getOutputStream()); beConnected = true; } catch (IOException e) { System.out.println("无法获取连接的输入输出流"); } } ...
if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) { throw new SecurityException(); } } catch (Exception e) { e.printStackTrace(); throw new ...
PrintStream ps = new PrintStream(s.getOutputStream()); /* 产生发消息的时刻 */ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); /* 向客户端发消息,消息...
String s=this.jTextField3.getText().trim(); sendData(s); } } private void listenClient(int port){//侦听 try{ if(jButton1.getText().trim().equals("侦听")){ server = new ServerSocket(port); ...
dataOut = new ObjectOutputStream(socket.getOutputStream()); listener = new Thread(this); listener.start(); } catch (IOException ioException) { ioException.printStackTrace(); } } } public ...
for (int i = 0; i; i++){ // 设置压缩文件入口entry,为被读取的文件创建压缩条目 File tempFile = new File(fileArray[i].toString()); String rootStr = file.getPath(); String entryStr = ...
heads是excel表头,datalist是数据public void createExcelStream(HttpServletResponse response,String filename,String[] heads,List[]> datalist){ try{ OutputStream os = response.getOutputStream();...
this.client=c; } public void run(){ try{ BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out=new PrintWriter(client.getOutputStream...