File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet displaying an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet displaying an image" Watch "Servlet displaying an image" New topic

Servlet displaying an image

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I want to use my servlet as the src for an <img> tag in a jsp page. That servlet should display a picture, that is not located in the webapp.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The short answer is that you need to get a java.awt.Image and a java.awt.Graphics context to draw on it, then get a separate package to convert it into the image type of choice.
I use the com.sun.image.codec.jpeg.* package that comes with Java, but it's unsupported and generally not recommended. You'd be able to find others out there.
The slightly longer answer is:

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
Thanks for your help.
Along with this website and your help
I can do a servlet called by an <IMG SRC> tag, but it shows a pic (Black box), but now I dont know how to use my own image calling the BufferedImage img or Image img class in java.
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
Ok, I got it to work, but what about with GIFs?
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
// FOR GIF, change content type accordingly. I'm not sure what's the content type of GIF files.
import javax.servlet.*;
import javax.servlet.http.*;
public class ImageLoader extends HttpServlet{
public ImageLoader() {}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
File image = new File("/home/chu/image01.jpg");
FileInputStream fis = new FileInputStream(image);
OutputStream out = res.getOutputStream();
DataOutputStream ds = new DataOutputStream(out);
byte b[] = new byte [32*1024];
while ( != -1){
I agree. Here's the link:
subject: Servlet displaying an image
It's not a secret anymore!