Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

invoking servlet in img tag

Status
Not open for further replies.

redlynne

Programmer
May 29, 2003
15
PH
Hi. I'm downloading an image stored in MS SQL database.
I have created a servlet that will somehow read and write the binary data extracted from the database. But my jsp cannot display the image in the page.
Can anyone please help me on how i can invoke a servlet outputing an image <img src=&quot;&quot;> tag?
 
How about if your servlet writes the image out as a file in a temp directory?

Then you only need to have the link to the file in your jsp...
Code:
<img src=<%=getPictureUrl()%>>
 
This is a servlet I use for my own. You can easily modify it to read the binary date from your database. Important part is the contenttype you send out. It should be image/jpeg or image/gif.

Extract:
import javax.servlet.http.*;
import javax.servlet.*;

import com.hd.common.web.servlet.*;
import java.io.IOException;
import java.io.*;

/** A servlet that reads a GIF file off the local system
* and sends it to the client with the appropriate MIME type.
* Includes the Content-Length header to support the
* use of persistent HTTP connections unless explicitly
* instructed not to through &quot;usePersistence=no&quot;.
* Used by the PersistentConnection servlet.
*/

/**
* <p>Title: ImageRetrieverServlet</p>
* <p>Description: retrieves a image from a permanent source</p>
* <p>Copyright: Copyright (c) 2003</p>
* @author Fabian Dierckxsens
* @version 1.0
*/

public class ImageRetrieverServlet extends HttpServlet {

private static String sourcePath = &quot;c:/&quot;;

/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException {
/**
* This call is critical! The ServletConfig object is used elsewhere in
* the servlet, and the init method of the superclass registers it where
* the servlet can find it later.
*/
super.init(config);

try {
sourcePath = config.getInitParameter(&quot;sourcePath&quot;);
}
catch (Exception ex) {
System.out.println(ex);
}
}


public ImageRetrieverServlet() {
}

public void doGet (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

String picLocation = request.getParameter(&quot;picLocation&quot;);

if ((picLocation == null)
|| (picLocation.length() == 0)) {
reportError(response, &quot;Image File Not Specified&quot;);
return;
}
//String file = sourcePath + getServletContext().getRealPath(picLocation);
String file = sourcePath + (picLocation.startsWith(&quot;/&quot;) ? &quot;&quot; : &quot;/&quot;) + picLocation;
try {

BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
int imageByte;
while((imageByte = in.read()) != -1) {
byteStream.write(imageByte);
}
in.close();
String persistenceFlag =
request.getParameter(&quot;usePersistence&quot;);
boolean usePersistence =
( (persistenceFlag == null)
|| (!persistenceFlag.equals(&quot;no&quot;)));

response.setContentType((file.endsWith(&quot;.gif&quot;) ? &quot;image/gif&quot; : &quot;image/jpeg&quot;));
if (usePersistence) {
response.setContentLength(byteStream.size());
}
byteStream.writeTo(response.getOutputStream());
}
catch(IOException ioe) {
reportError(response, &quot;Error: &quot; + ioe);
}
}
public void reportError(HttpServletResponse response, String message)
throws IOException {

response.sendError(response.SC_NOT_FOUND, message);
}
}

Should be helpfull
Fabian Dierckxsens
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top