0

我想从数据库检索图像并将其显示到 jsp 页面。所以我创建了一个jsp页面,但我尝试了很多错误但无法修复它。我可以在 servlet 中轻松做到这一点,但我需要在 jsp 中

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %> 
<%@page import ="DB.*" %>

<%// declare a connection by using Connection interface Connection connection = null;
/* Create string of connection url within specified format with machine 
name, port number and database name. Here machine name id localhost 
and database name is mahendra. */
Blob image = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
DataBaseConnection db= new DataBaseConnection();
ServletOutputStream out1 = response.getOutputStream();
try {
Class.forName("com.mysql.jdbc.Driver");
con=db.connet();
stmt = con.createStatement();
rs = stmt.executeQuery("select img from one where  id = '4'");
if (rs.next()) {
image = rs.getBlob(1);
} else {
response.setContentType("text/html");

out.println("<font color='red'>image not found for given id</font>");

return;
}
response.setContentType("image/gif");
InputStream in = image.getBinaryStream();
int length = (int) image.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
out1.write(buffer, 0, length);
}
in.close();
out.flush();

} catch (Exception e) {
response.setContentType("text/html");
out.println("<html><head><title>Unable To Display image</title></head>");
out.println("<body><h4><font color='red'>Image Display Error=" + e.getMessage() +
"</font></h4></body></html>");
return;
} 


%>

错误是

HTTP Status 500 - java.lang.IllegalStateException: getOutputStream() has already been called for this response

type Exception report

message java.lang.IllegalStateException: getOutputStream() has already been called for this response

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.IllegalStateException: getOutputStream() has already been called for this response
    org.apache.catalina.connector.Response.getWriter(Response.java:639)
    org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
    org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
    org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
    org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
    org.apache.jsp.retrieveImage_jsp._jspService(retrieveImage_jsp.java:123)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.37 logs.

Apache Tomcat/7.0.37
4

2 回答 2

0

这是因为您的 JSP 中有此代码。

ServletOutputStream out1 = response.getOutputStream();

一个好的经验法则是不要ServletOutputStream在 JSP 中使用

附带说明:使用 Servlet 执行此类任务并使用 JSP 进行演示,其次,response.setContentType();在编写响应之前进行设置。

于 2013-04-04T12:25:32.127 回答
0

在 servlet 中,您可以创建输出流对象。但是在 jsp 中,不能创建 outputstream 对象。取而代之的是,您必须使用隐式对象 out。这就是为什么它抛出错误,因为 getOutputStream() 方法已经被调用。所以删除该行,并使用隐式对象。

于 2013-04-04T12:28:09.807 回答