Announcement Announcement Module
No announcement yet.
Servlet writing byte[] image in jsp page Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Servlet writing byte[] image in jsp page

    I would be very greatful if someone could take time to comment....

    I have been playing with the petclinic example that comes with the springframwork distribution and have added some functionality.

    I have a webflow where Im suppose to upload an image on page one and show the image and text on page 2 and confirm and save on DB on page 3. Its no problem to upload and save the image (byte[]) in the mysql database but the problem is to show the image on page 2.

    In my jsp-page where Im suppose to show the image Im calling a servlet that is suppose to print out the (byte[]) that attached in the request brings from page 1 but I get an illegalStateException when I call the "ServletOutputStream out = res.getOutputStream();" method..... ?

    I have studied the imagedb example without seeing any usable content to apply on my application. I have also tryed many different angles and examples and searched the web.

    the jsp page 1 that will upload the image.
    The class ad holds the byte[] attribute "theByteImage" which have getters and setters. Im only showing the main tags from jsp-page 1 here.

    <form method="post" enctype="multipart/form-data">
    <spring:bind path="ad.theByteImage">
    <FONT color="red">
    <B><c:out value="${status.errorMessage}"/></B>
    <BR><input type="file" name="theByteImage"/>

    The jsp page that is suppose to show the image looks like this:
    <form method="post">
    <img src="/Image"/>

    The Image servlet looks like this:
    public class Image extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    Ad ad = (Ad)req.getAttribute("ad");
    if (FileUpload.isMultipartContent(req)){
    byte[] image = ad.getTheByteImage();
    if (image.length > 0){
    ServletOutputStream out = res.getOutputStream();
    }// if
    }// if
    }catch(Exception e){
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    System.out.println("=========servlet dopost....");

    My web.xml looks lite this:
    <servlet-class>org.springframework.samples.petclinic.util.I mage</servlet-class>

    My applicationContext-hibernate holds the multipartResolver:
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver">
    <property name="maxUploadSize">

    The main problem is that I get an java.lang.illegalStateException in the servlet when it comes to the "ServletOutputStream out = res.getOutputStream();" code but if you have suggestions about the code and if I missed something. Im way into my second week trying to solve this problem and I see many other have the same postings eaven though I haven't seen any solution printing out an byte[] in the servlet....


  • #2
    How is the Image servlet being referenced? While the JSP/HTML looks ok to me, you will either need to store the Ad as a session attribute or pass it as a parameter and look it up (unless you are using a Filter). While I can't see enough to be sure, it seems to me that the most probable cause is that you are attempting to use one request/response cycle to both upload the image and display it - if this is what you are doing you simply need to upload the image to one Controller which returns a page view, and then reference the Image servlet path as you are - at this point the request will not contain Multipart content. If this is not what is going on please post some of your stack trace and any other details you can think of.


    • #3
      Hi guys

      I have similar problem here. Does anyone knows some good tutorial about displaying image, derived from data base, on jsp page ?