This discussion is archived
1 Reply Latest reply: Mar 9, 2011 2:38 AM by gimbal2 RSS

How to display image using servlet

833638 Newbie
Currently Being Moderated
Hi,

I have a requirement wherein i have to display images on a page,which are stored as a BLOB field in the DB.Initially i was trying to do it with JSp but then read in the net that JSP is not that deirable as far as binaryoutput is concerned so i changed my design now from a JSP page iam displaying a link and am using a servlet to fetch-convert-display the images,but my problem is that no image is being displayed and no error is thrown ,iam simply receiving a blank screen.The files i have made for this are as below:-

CustSifReceiver.jsp

<html>
<body>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page language="Java" %>


          <%
String sourceCode=request.getParameter("sourcecode");
          String refType=request.getParameter("reftype");
          String refNo=request.getParameter("refno");
          String branchCode=request.getParameter("branchcode");
     String sigId=request.getParameter("sigid");
          response.setContentType("text/plain");
          
          //String contentType=request.getContentType();
          //out.println(contentType);
          /*out.println(sourceCode+" "+refType+" "+refNo+" "+branchCode+" "+sigId);
          if(refType.equals("A"))
          out.println("Branchcode empty");*/
          if(sourceCode.equals("")){
          %>
          
               <jsp:forward page="dispError.jsp">
                    <jsp:param name="msg" value="Source Code is null" />
               </jsp:forward>
          
          <%
          }
          if(refType.equals("")){
          %>
          
               <jsp:forward page="dispError.jsp" >
                    <jsp:param name="msg" value="Reference Type is null" />
               </jsp:forward>
          <%
          }
          if(refNo.equals("")){
          %>
               <jsp:forward page="dispError.jsp" >
                    <jsp:param name="msg" value="Reference Number is null" />
               </jsp:forward>
          <%
          }
          if(refType.equals("A") && branchCode.equals("")){
               
          %>
               <jsp:forward page="dispError.jsp" >
                    <jsp:param name="msg" value="Branch Code can not be null for reference type Account" />
               </jsp:forward>
          <%
          }
          Connection connection = null;
String connectionURL = "jdbc:oracle:thin:@10.184.74.107:1525:ME11GR2";
          String fetchAccCustomer,tableName,fetchCustSig,fetchAccSig;
ResultSet rs = null,rsAccount=null,rsCust=null;
PreparedStatement psmnt = null;
try {  
               //DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
               
connection = DriverManager.getConnection(connectionURL, "devf1mb", "devf1mb");
               if(refType.equals("C")){
               tableName="svtm_cif_sig_master";
               fetchAccCustomer="select * from "+tableName+" where cif_id like '"+refNo+"'";
               }
               else{
               tableName="svtm_acc_sig_master";
               fetchAccCustomer="select * from "+tableName+" where acc_no like '"+refNo+"'";
               }
               //psmnt=psmnt.executeQuery();
//String query = "select CIF_ID,cif_sig_id,specimen_no from svtm_cif_sig_det where cif_id ='"+CifID+"'";
//System.out.println("query:- "+query);
psmnt = connection.prepareStatement(fetchAccCustomer);
rs = psmnt.executeQuery();
               if(rs==null){
               %>
                    <jsp:forward page="dispError.jsp" >
                         <jsp:param name="msg" value="Account Number/Customer Does not exist" />
                    </jsp:forward>
               <%     
               }
               if(refType.equals("C")){
                    if(sigId!=""){
                         fetchCustSig="select * from svtm_cif_sig_master inner join svtm_cif_sig_det using (cif_id,cif_sig_id) where cif_id like '"+refNo+"'";
                         psmnt = connection.prepareStatement(fetchCustSig);
                         rs = psmnt.executeQuery();
                         if(rs==null){
                         %>
                              <jsp:forward page="dispError.jsp" >
                                   <jsp:param name="msg" value="Signature Id does not exist" />
                              </jsp:forward>
                         <%
                         }
                    
                    }
                    else{
                         fetchCustSig="select * from svtm_cif_sig_master inner join svtm_cif_sig_det using (cif_id,cif_sig_id) where cif_id like '"+refNo+"'";
                         psmnt = connection.prepareStatement(fetchCustSig);
                         rs = psmnt.executeQuery();
                         if(rs.equals(null)){
                         %>
                              <jsp:forward page="dispError.jsp" >
                                   <jsp:param name="msg" value="Source Code is null" />
                              </jsp:forward>
                         <%
                         }
                    }
               }
               else{
                    if(sigId!=""){
                         fetchAccSig="select * from svtm_acc_sig_det inner join svtm_cif_sig_det using (cif_sig_id) where acc_no like '"+refNo+"' and branch like '"+branchCode+"' and cif_sig_id like '"+sigId+"'";
                         psmnt = connection.prepareStatement(fetchAccSig);
                         rs = psmnt.executeQuery();
                         if(rs==null){
                         %>
                              <jsp:forward page="dispError.jsp" >
                                   <jsp:param name="msg" value="Signature Id does not exist" />
                              </jsp:forward>
                         <%     
                         }
                         
                    }
                    else{
                         fetchAccSig="select * from svtm_acc_sig_det inner join svtm_cif_sig_det using (cif_sig_id) where acc_no like '"+refNo+"' and branch like '"+branchCode+"'";
                         psmnt = connection.prepareStatement(fetchAccSig);
                         rs = psmnt.executeQuery();
                    }
               }
               if(refType.equals("A")){
               String accountDescription="select * from sttm_cust_account a inner join svtm_acc_sig_master b on a.cust_ac_no=b.acc_no where a.cust_ac_no = '"+refNo+"'";
               psmnt = connection.prepareStatement(accountDescription);
               rsAccount=psmnt.executeQuery();
               }
               else{
               String customerName="select * from sttm_customer where customer_no = '"+refNo+"'";
               psmnt = connection.prepareStatement(customerName);
               rsCust=psmnt.executeQuery();
               
               }
          

out.println("<TABLE ALIGN='CENTRE' BORDER='1'><TR><TH>HEADER SECTION</TH></TR>");
               
               if(refType.equals("A")){
               while(rsAccount.next()){
               out.println("<TR><TD>Account Branch</TD><TD>"+branchCode+"</TD></TR>");
               out.println("<TR><TD>Account Number</TD><TD>"+refNo+"</TD></TR>");
               out.println("<TR><TD>Account Description</TD><TD>"+rsAccount.getString("ac_desc")+"</TD></TR>");
               out.println("<TR><TD>Minimum Number of Signatures</TD><TD>"+rsAccount.getString("min_no_of_sig")+"</TD></TR>");
               out.println("<TR><TD>Account Message</TD><TD>"+rsAccount.getString("acc_msg")+"</TD></TR>");
               }
               }
               if(refType.equals("C")){
               while(rsCust.next()){
               out.println("<TR><TD>Customer Name</TD><TD>"+rsCust.getString("customer_name1")+"</TD></TR>");
               out.println("<TR><TD>Customer Number</TD><TD>"+rsCust.getString("customer_no")+"</TD></TR>");
               }
               }
               
               out.println("</TABLE>");
               out.println("<BR/><BR/><BR/>");
     

               if(refType.equals("A")){
               

               out.println("<TABLE ALIGN='CENTRE' BORDER='1'><TR><TH>DETAIL SECTION</TH></TR>");
               out.println("<TR width='100%'><TD><TABLE ALIGN='CENTRE' BORDER='1'><TR><TH>SIGNATURE ID</TH><TH>NAME</TH><TH>TYPE</TH><TH>APPROVAL LIMIT</TH><TH>SIGNATORY MESSAGE</TH><TH>IMAGE</TH></TR>");
               while(rs.next()){
                              out.println("<TR><TD>"+rs.getString("cif_sig_id")+"</TD><TD>"+rs.getString("cif_sig_name")+"</TD><TD>"+rs.getString("sig_type")+"</TD><TD>"+rs.getString("approval_limit")+"</TD><TD>"+rs.getString("sig_msg")+"</TD>");
                              out.println("<TD>");
                              out.print("<img src='/showSig.jsp?refNo="+ rs.getString("cif_id"));
                              out.print("&refType="+refType);
                              out.print("&branchCode="+rs.getString("branch"));
                              out.print("&sigId="+rs.getString("cif_sig_id"));
                              out.print("&specimenId="+rs.getString("specimen_no"));
                              out.print("'></img>");
               /*InputStream sImage;
               byte[] bytearray = new byte[1048576];
               int size=0;
sImage = rs.getBinaryStream("SIGN_IMG");
               size=sImage.read(bytearray);
               out.println(size);
               response.setContentType("image/jpeg");
               while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
}
                    response.setContentType("text/plain");*/

                    out.println("</TD></TR>");
               }
               out.println("</TABLE></TD></TR>");
               out.println("</TABLE>");
               }
               
          else if(refType.equals("C")){
               out.println("<TABLE ALIGN='CENTRE' BORDER='1'><TR><TH>DETAIL SECTION</TH></TR>");
               out.println("<TR width='100%'><TD><TABLE ALIGN='CENTRE' BORDER='1'><TR><TH>SIGNATURE ID</TH><TH>SIGNATURE NAME</TH><TH>SIGNATURE TITLE</TH><TH>IMAGE</TH></TR>");
               while(rs.next()){

                         out.println("<TR><TD>"+rs.getString("cif_sig_id")+"</TD><TD>"+rs.getString("cif_sig_name")+"</TD><TD>"+rs.getString("cif_sig_title")+"</TD>");
                         out.println("<TD>");
                         out.print("<a href='/SignatureDisp?refNo="+ rs.getString("cif_id"));
                         out.print("&refType="+refType);
                         //out.print("&branchCode="+rs.getString("branch"));
                         out.print("&sigId="+rs.getString("cif_sig_id"));
                         out.print("&specimenId="+rs.getString("specimen_no"));
                         out.print("'>Signature</a>");
               /*InputStream sImage;
               byte[] bytearray = new byte[1048576];
               int size=0;
sImage = rs.getBinaryStream("SIGN_IMG");
               size=sImage.read(bytearray);
               out.println(size);
               response.setContentType("image/jpeg");
               while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
}
                    response.setContentType("text/plain");*/
                    out.println("</TD></TR>");
               }
               out.println("</TABLE></TD></TR>");
               out.println("</TABLE>");
     }
               
out.println("</BODY></HTML>");
     
     }
catch(Exception ex){
out.println("error :"+ex);
               //psmnt.close();
               //rs.close();
               //rsCust.close();
//rsAccount.close();
//connection.close();
                    

}
     finally {
// close all the connections.
                    

}           
%>
</body>
</html>
     



SignatureDisp.java


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.sql.Connection;
import oracle.jdbc.OracleDriver;
public class SignatureDisp extends HttpServlet{
     
     public void init(ServletConfig config) throws ServletException
     {
          super.init(config);
          
     }
     
     public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
          Connection connection=null;
          PreparedStatement pstmt=null;
          OutputStream out=res.getOutputStream();
          Blob image;
          ResultSet rSet=null;
          byte []imgData=null;
          String cifNo=req.getParameter("refNo");
          String specNo=req.getParameter("specimenId");
          String query="select SIGN_IMG from svtm_cif_sig_det using (cif_sig_id) where cif_id like '"+cifNo+"'"+" and cif_sig_id like '"+specNo+"'";
          String connString="jdbc:oracle:thin:@10.184.74.107:1525:ME11GR2";
          try{
          Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
          connection=DriverManager.getConnection(connString,"devf1mb","devf1mb");
          pstmt=connection.prepareStatement(query);
          rSet=pstmt.executeQuery();
          res.setContentType("image/jpeg");
          while(rSet.next()){
               image=rSet.getBlob(1);
               imgData=image.getBytes(1,(int)(image.length()));
               out.flush();
               out.write(imgData);
          }
          
          
          }
          catch(Exception e){
               e.printStackTrace();
          }
          finally{
               try{
               connection.close();
               rSet.close();
               pstmt.close();
               out.close();
               }
               catch(Exception e){
                    e.printStackTrace();
               }
          }
          
     }

}




PLz help
  • 1. Re: How to display image using servlet
    gimbal2 Guru
    Currently Being Moderated
    so much wrong with this post...

    a) use the code tags to format the code. Now it is an unreadable mess
    b) scriptlets in your JSP. I don't even want to look at that mess. Learn how to combine servlets and JSPs to create clean, readable and maintainable code in which view logic and business logic are separated. The rule: no java code in your JSP, only tags and EL expressions (learn about JSTL).


    So you get a blank screen. When you do "view source" in your browser, do you see anything there? If nothing, check if you are behind a proxy server. The solution to a post of not too long ago was that the blank screen was being caused by faulty proxy server settings (the user figured it out himself, I don't know what was done to solve it).


    If all this fails you will have to do some debugging magic using your favorite IDE. It will probably be something stupid you are overlooking in the code. It is annoying, but debugging code that does not work is a big part of your job. Better get good at it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points