Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to display image using servlet

833638Mar 9 2011 — edited Mar 9 2011
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 6 2011
Added on Mar 9 2011
1 comment
1,119 views