0

I am fetching data from a database using a servlet and displaying in a JSP. I am getting the String, but not getting an image.

I am getting output like this: English [B@12fd16f

Here is my servlet class:

@WebServlet("/ReadFindServlet")
 public class ReadFindServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private List<Find> findList = new ArrayList<>();    

public List<Find> getFindList() {
    return findList;
}

public void setFindList(List<Find> findList) {
    this.findList = findList;
}   

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    ResultSet resultSet = null;

    try {

    resultSet = LoginDao.readFind();

    while(resultSet.next()){
        Find find = new Find();

        find.setSubject(resultSet.getString(1));                
        find.setImage(resultSet.getBytes(2));

        response.setContentType("image/jpeg");              


        findList.add(find);

    }
    setFindList(findList);          

    request.setAttribute("findList", findList);
    RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/find.jsp");
    requestDispatcher.forward(request, response);

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}
}

Find.java

public class Find {

private String subject;
private byte[] image;

public String getSubject() {
    return subject;
}
public void setSubject(String subject) {
    this.subject = subject;
}
public byte[] getImage() {
    return image;
}
public void setImage(byte[] image) {
    this.image = image;
}

}

jsp -

<c:forEach items="${findList}" var="findRecord">
     <h1>Here are details - </h1>                 
     ${findRecord.subject}                 
     <img src="/ReadFindServlet"${findRecord.image }/>
</c:forEach>

I have tried this also -

    <img src="/ReadFindServlet/${findRecord.image }"/>
    <img src="{findRecord.image }"/>

But didn't work.

please help.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
jay thakur
  • 151
  • 2
  • 13
  • You can't insert binary data in the `src` attribute of the `` tag. The `src` attribute specifies a URL, which the browser will use to retrieve the binary data *separately*. – Andreas May 06 '16 at 06:55
  • create a servlet which streams back a byte array of the image. This can be then be called using ``. Of course this url can also be tested just as a normal url in a browser – Scary Wombat May 06 '16 at 07:01
  • @Andreas. Thanks for reply. How to do this. please suggest & also how should i store image & retrieve. I am storing as largeblob. – jay thakur May 06 '16 at 07:06
  • @ScaryWombat please suggest how to "create a servlet which streams back a byte array of the image" . thanks – jay thakur May 06 '16 at 07:09
  • @jaythakur as you dont seem to be able to use Google, here you http://balusc.omnifaces.org/2007/04/imageservlet.html – Scary Wombat May 06 '16 at 07:17
  • or https://thelogicalwebmaster.wordpress.com/2011/03/02/stream-and-serve-with-java-servlets/ – Scary Wombat May 06 '16 at 07:18
  • @ScaryWombat. thank you so much. – jay thakur May 06 '16 at 07:25
  • @ScaryWombat. thanks. Now i am getting string & image both. I made another servlet for image calling. But another problem i am facing here that when i am refreshing the jsp page, I am getting same data again again i:e; duplicate data. how to solve this ? please suggest. – jay thakur May 11 '16 at 07:37

0 Answers0