0

Hi guys I am trying to get images from database and display it in JSP but when i passed my imageId inside it will call servlet but override image here is my code

<c:forEach var="row" items="${result.rows }">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs 12 well-profile">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs 12 ">
                        <span class="text-name"><c:out value="${row.Name }"></c:out></span>
                        <span class="text-xs col-md-offset-6 col-xs-offset-6"> Profile Created By: <c:out value="${row.ProfileBy }"></c:out></span>
                    </div>  
                    <div class="col-lg-3 col-md-3 col-sm-12 col-xs 12">
                        <img class="img-responsive well-image" src="${pageContext.servletContext.contextPath }/PhotoServlet?id=${row.id}" />

                    </div>
              </div>
</c:forEach>

My Servlet Code

id=request.getParameter("id");
System.out.println("Photo Id:"+id);

try{

        con=DBUtility.getConnection();
        stmt=con.prepareStatement("select Photo From personalinfo where  id=?");
        stmt.setString(1, id);

        rs=stmt.executeQuery();
        if(rs.next()){
            response.getOutputStream().write(rs.getBytes("Photo"));
        }
    }catch(SQLException ex){
        ex.printStackTrace();
    }finally{
        DBUtility.closeConnection();
    }

JSP page sometime Shows different image based on id and sometimes it will show same image for all.

override image

sometime get correct

Nadeem Mohammed
  • 559
  • 1
  • 6
  • 14
  • 1) What does _but override image_ mean? 2) You should [set some headers](http://stackoverflow.com/a/1812356/3511123) in your servlet, at least `Content-Type` (using `response.setContentType()` method). – Jozef Chocholacek Sep 02 '16 at 06:31
  • it will print image associate with last id for different names. – Nadeem Mohammed Sep 02 '16 at 06:42
  • When the problem occurs, does the generated HTML contain proper URLs in images' `src` attribute? (I would suggest so, the JSP looks fine - this seems to me like a problem with browser cache or so...) – Jozef Chocholacek Sep 02 '16 at 06:58
  • can its a problem of timing of calling servlet and direct print resultset. – Nadeem Mohammed Sep 02 '16 at 07:08
  • Your servlet is clearly not thread safe. This is a classic major beginner's mistake. See the duplicate to learn how servlets really work and how you should write thread safe servlet code. – BalusC Sep 02 '16 at 07:40
  • Thanks BalusC Its work for me when i used thread safe servlet. – Nadeem Mohammed Sep 02 '16 at 09:07

0 Answers0