Display an image on jsp page
Hi all, after retrieve an image from MsSQL database i want to display it on the jsp page (see the coding in below ). however i accepted some error messages.
<%
response.reset();
response.setContentType("image/gif");
InputStream in = movie.getPoster();
ServletOutputStream sos = response.getOutputStream();
int len = 0;
byte[] b =newbyte[1024];
len = in.read(b);
sos.write(b,0,len);
sos.close();
%>
<********************ERROR Message****************>
SRVE0026E: [Servlet Error]-[OutputStream already obtained]: java.lang.IllegalStateException: OutputStream already obtained
<*********************END******************************>
I read some articles from internet, the possible way to solve this problem is to make a servlet for this page, then i did so. But this time the error message indicates that "the writer is already obtained".
<***************ERROR MESSAGE ****************>
E SRVE0026E: [Servlet Error]-[MovieMain]: java.lang.IllegalStateException: Writer already obtained
<***************END *********************************>
So, Would any one give some suggestions to me.
any reply will be a deep appreciates.
[1449 byte] By [
king_konga] at [2007-10-2 18:27:15]

write the code for image generation in a servlet and in jsp write<img src="/servletName">
Thanks for your reply,
I created a seperate servlet for only loading the image. there might be somethings wrong with the loadimage servlet because it couldn't display the image on jsp page, however there is no more exception throw this time.
In LoadImage Servlet:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
Movie movie =(Movie)req.getAttribute("a_movie");
try
{
resp.reset();
resp.setContentType("image/gif");
ServletOutputStream sos = resp.getOutputStream();
byte[] b = movie.getPoster();
int len = b.length;
sos.write(b,0,len);
sos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
In JSP:
<IMG src="<%=request.getContextPath() %>/LoadImage">
>however there is no more exception throw this time.
How do you know?
You catch block just prints a stack trace to the log. Use
catch(Exception e){
throw new ServletException("Unable to print image"+e.getMessage());
}
Also try explicitly calling flush() on the output stream after you are finished writing the response.
ram.
Use DataOutputStream instead of ServletOutputStream DataOutputStream out = new DataOutputStream(resp.getOutputStream());ge.write(out);out.close();
sorry!!!sos.write(out);not the ge.write(out);
I C, i have amended my code as below, but still couldn't get the image on page.
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
Movie movie =(Movie)req.getSession().getAttribute("a_movie");
try
{
resp.reset();
resp.setContentType("image/gif");
DataOutputStream out = new DataOutputStream(resp.getOutputStream());
byte[] b = movie.getPoster();
int len = b.length;
out.write(b,0,len);
out.flush();
out.close();
}
catch(Exception e)
{
throw new ServletException("Unable to print image"+e.getMessage());
}
}
did you tried this url: http://server:prt/servlet
Give me ur mail id... i can send u a working code. May be u can check with that.
There is nothing wrong with the URL of LoadImage Servlet, I ran the servlet line by line under debug mode. At least the LoadImage servlet is called by statement "<IMG src="<%=request.getContextPath() %>/LoadImage">" on jsp.
masker@ctgdie.comreally appreciate for your help!
I just wanted to make sure that you are able to see the image derectly from servlet.
What do you mean by "able to see the image derectly from servlet"?
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Get the absolute path of the image
ServletContext sc = getServletContext();
String filename = sc.getRealPath("image.gif");
// Get the MIME type of the image
String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
sc.log("Could not get MIME type of "+filename);
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
// Set content type
resp.setContentType(mimeType);
// Set content size
File file = new File(filename);
resp.setContentLength((int)file.length());
// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}