• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsp to display image

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
assuming i have read an image from MS SQL w/ image datatype field.
save that to a Byte[] or maybe ByteArrayOutputStream type.
let's say i saved it to a request:
request.setAttribute("datastream",readdata);
my question is how can i display this data as an image from a jsp.
i have this jsp but is seem's id doesn't work.
will anyone help me. thanks a lot
<%@ page language="java" %>
<%@ page buffer="64kb" %>
<%
response.setContentType((String)request.getAttribute("contentType"));
//response.setHeader("Content-Type",(String)request.getAttribute("contentType"));
//response.addHeader("Content-Disposition","filename=image.jpg");
%>
<%= request.getAttribute("datastream") %>
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSPs are best suited for text data. Why not consider a servlet rather than JSP to serve your image?
hth,
bear
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'll try that and get to back to you soon...
thanks...
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now i have a trouble.... how do i print an InputStream in a servlet.
thanks.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jay
In a servlet you need to write the contents of your byte array to the output stream on the response object.

HTH
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
somebody ask me how i was able to display the image from the database. so, im posting the code here just in case someone needs it.
you just need the type of the image (i.e. image/gif) plus the image data sa InputStream.
my servlet is: >>>>>
public class ShowImage extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws IOException, ServletException
{
System.err.println("\n\tEntering ShowImage...");
String id = (String)req.getParameter("id");
String imagenum = (String)req.getParameter("field");
System.err.println("param:id="+id+",field="+imagenum);
ProductService service = new ProductService();
ArrayList imagedata = service.getProductImage(id,imagenum);
String type = (String) imagedata.get(0);
InputStream data = (InputStream) imagedata.get(1);
resp.setContentType(type);
resp.setStatus(HttpServletResponse.SC_OK);
System.err.println("\ttype="+type);
System.err.println("\tdata="+data);
ServletOutputStream sout = resp.getOutputStream();
int c;
while((c = data.read()) != -1)
{
sout.write(c);
}
data.close();
sout.close();
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws IOException, ServletException
{
doGet(req,resp);
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would be considerably faster if you read and wrote byte[] buffers. For small images, get the size, create a byte[] that size and you can do one read and one write.
Bill
reply
    Bookmark Topic Watch Topic
  • New Topic