Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp to display image  RSS feed

 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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") %>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
now i have a trouble.... how do i print an InputStream in a servlet.
thanks.
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks a lot
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!