0

I want to retrieve image from database which is stored as a blob and this image is a profile image of employee so with image some other details will come like name,designation and all so i am passing servlet path and value in employeedetail.jsp and it is going to that servlet and everything is correct like query , parameter but finally on employeedetail.jsp page image is not coming it is just showing a cross sign there please anyone help me here is code..

Login.Java

public class Login extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        HttpSession session = request.getSession();
        // InputStream sImage;
        String sid = request.getParameter("eid");
        session.setAttribute("sid", sid);
        String password = request.getParameter("password");

        try {
            Connection con = ConnectionManager.getConnection();

            String query = "select eid, name, password, sex, dob, bloodgroup, fathername, qualification, mailid, contactnum, skills, temporaryadd, permanentadd, access_type
            from empinfo where eid = '" + sid+ "' AND password = '" + password + "' ";

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            if (rs.next()) {
                String n1 = rs.getString("eid");
                String n2 = rs.getString("name");
                String n3 = rs.getString("password");
                String n4 = rs.getString("sex");
                String n5 = rs.getString("dob");
                String n6 = rs.getString("bloodgroup");
                String n7 = rs.getString("fathername");
                String n8 = rs.getString("qualification");
                String n9 = rs.getString("mailid");
                String n10 = rs.getString("contactnum");
                String n11 = rs.getString("skills");
                String n12 = rs.getString("temporaryadd");
                String n13 = rs.getString("permanentadd");
                String n14 = rs.getString("access_type");
                //  sImage = rs.getBinaryStream("image");
                session.setAttribute("eid", n1);
                session.setAttribute("name", n2);
                session.setAttribute("password", n3);
                session.setAttribute("sex", n4);
                session.setAttribute("dob", n5);
                session.setAttribute("bloodgroup", n6);
                session.setAttribute("fathername", n7);
                session.setAttribute("qualification", n8);
                session.setAttribute("mailid", n9);
                session.setAttribute("contactnum", n10);
                session.setAttribute("skills", n11);
                session.setAttribute("temporaryadd", n12);
                session.setAttribute("permanentadd", n13);
                session.setAttribute("access_type", n14);
                //session.setAttribute("image",sImage);
                response.sendRedirect("EmployeeDetail.jsp");
            } else {

                String message = "Your are not a registered employee.";
                request.setAttribute("message", message);
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
        } catch (Throwable theException) {
            System.out.println(theException);
        }
    }
}

Employeedetail.jsp

<html>
<head>
  <style>
  </style>
</head>
<body>
<table align=center cellspacing=1 cellpadding=3>
  <tr><Td> Employee name</td><Td><%=name%></td></tr>
  <tr>
  <td><img src="${pageContext.servletContext.contextPath }/retImage?param1=  <%=eid%>">image</td>
    // here i am passing the value and it is passing to servlet also
  </tr>
  <tr><Td>Password</td><Td><%=password%></td></tr>
  <tr><Td>Sex</td><Td><%=sex%></td></tr>
  <tr><Td>dob</td><Td><%=dob%></td></tr>
  <tr><Td>Blodgroup</td><Td><%=bloodgroup%></td></tr>
  <tr><Td>fathername</td><Td><%=fathername%></td></tr>
  <tr><Td>Qualification</td><Td><%=qualification%></td></tr>
  <tr><Td>mail id</td><Td><%=mailid%></td></tr>
  <tr><Td>contact num</td><Td><%=contactnum%></td></tr>
  <tr><Td>skills</td><Td><%=skills%></td></tr>
  <tr><Td>local address</td><Td><%=temporaryadd%></td></tr>
  <tr><Td>permanent Address</td><Td><%=permanentadd%></td></tr>
  <tr><Td>Emp Category</td><Td><%=access_type%></td></tr>
</table><BR>
</div>
</body>
</html>

retImage.java

public class retImage extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException
    {
        HttpSession session = request.getSession(true);

        String sid=(String)session.getAttribute("eid");

        session.setAttribute("sid",sid);
        System.out.println("sid is " +sid);    

        String iid = request.getParameter("param1");
        InputStream sImage;
        try {

            Connection con = ConnectionManager.getConnection();
            String Query= "SELECT image FROM empinfo WHERE eid ='"+iid+"'";
            System.out.println("Query is" +Query);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(Query);
            System.out.println("..........1");
            if(rs.next())
            {
                System.out.println("........2");

                byte[] bytearray = new byte[1048576];
                int size=0;

                sImage = rs.getBinaryStream(1);    
                response.reset();

                while((size=sImage.read(bytearray))!= -1 ) {
                    response.getOutputStream().write(bytearray,0,size);
                    System.out.println(".......3");
                }

            }
        }
        catch(Exception ex){
            System.out.println("error :"+ex);
        }
    }
}
sourav78611
  • 99
  • 2
  • 17
  • What happens if you call retImage?param1=someid? Do you get an exception or something else? – Jeroen Aug 05 '14 at 09:51
  • no sir infact i have given SOP in beteween many place all are workingt fine..except image is not getting displayed.. – sourav78611 Aug 05 '14 at 10:09
  • @sourav78611 Let me know if the duplicated answer didn't help you. – Braj Aug 05 '14 at 10:40
  • @sourav78611 OK reopened for others to answer. I am sorry. I'll try your code and will come back if any anything found. If you don't use `@user` then the comment is not delivered to the desired user. – Braj Aug 05 '14 at 11:01
  • my question is still open no body has given answer so please help here... – sourav78611 Aug 13 '14 at 08:07

1 Answers1

1

Add the finally block in retImage.java as shown below:-

public class retImage extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException
    {
       ......
       // replace InputStream sImage with sImageBytes
       byte[] sImageBytes;
        try {           
            .....
            if(rs.next())
            {
                ...
                sImageBytes = rs.getBytes()

                response.setContentType("image/jpeg");
                response.setContentLength(sImageBytes.length);
                // Give the name of the image in the name variable in the below line   
                response.setHeader("Content-Disposition", "inline; filename=\"" + name+ "\"");

                BufferedInputStream  input = new BufferedInputStream(new ByteArrayInputStream(sImageBytes));
                BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());

                byte[] buffer = new byte[8192];
                int length;
                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                    System.out.println(".......3");
                }
            }
        }
        catch(Exception ex){
            System.out.println("error :"+ex);
        }
    }
}
Manjunath
  • 1,685
  • 9
  • 9
  • are you able to see the System.out.println(".......3")? And what are you storing in the image column is it the bytes or the inputstream which you did earlier? – Manjunath Aug 19 '14 at 08:54
  • yes bro System.out.println(".......3") this line is coming and it is last yea it is same InputStream but here its not mvc and i am simply retrieving image in binary stram just review my code you wil understand.. – sourav78611 Aug 19 '14 at 09:09
  • @sourav78611 try this but double check to make sure you have saved the bytes of the image in the image column of empInfo table properly – Manjunath Aug 19 '14 at 09:54
  • sorry yes you need to mention the column index/name – Manjunath Aug 19 '14 at 10:33
  • its working in mvc i have used your suggestion but page is loading slow i will discuss that part by tomorrow and that is last task of this image issue.. – sourav78611 Aug 19 '14 at 11:20
  • @ Manjunath Anand bro i have post that problem here it is working still some more correction needed and tell me if any better way possible.. here is link http://stackoverflow.com/questions/25397158/retrieve-image-or-any-document-from-database-using-mvc-pattern-in-java – sourav78611 Aug 20 '14 at 05:18