File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to display JPEG file using Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to display JPEG file using Servlet" Watch "How to display JPEG file using Servlet" New topic
Author

How to display JPEG file using Servlet

Basheer Yunus
Greenhorn

Joined: Dec 24, 2007
Posts: 7
Hi Frnds,

I have created a Bar Chart using Jfreechat, the Bar Chat is a jpg file, Now I want to display this jpg file in browser. I am using <img src > tag for displaying jpg file in servlet but it is not displaying the jpg file always (I am using NetBeans 5.5).
[I have two files bar_chart3D.jpg & bar_chart.jpg - it is displaying the jpg when I give the src as bar_chart3D.jpg but not displaying jpg when I give bar_chart.jpg, I dont know the reason.]

I request masters to help me to display jpg files using servlet.

Thank you
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
Are you using the correct URLs when accessing those images? Are the permissions on the files set to allow public access?

You can start to diagnose the problem by using a tool like the LiveHttpHeaders extension for Firefox; it displays all image requests sent by the browser as well as the server's response.


Ping & DNS - my free Android networking tools app
Basheer Yunus
Greenhorn

Joined: Dec 24, 2007
Posts: 7
I am just running my servlet in netbeans which opens IE and displays the jpg file. I have placed the jpg file in the same directory where the servlet is residing (same package).

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<HTML><BODY>");
out.print("<img SRC=\"bar_chart.jpg\" ALT=\"Bar Chart\">");
out.println("</BODY>");
out.println("</HTML>");
.....
}


if I give bar_chart3D.jpg then it is displaying. So that means it may not be a problem with permissions.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
An image needs to be in a publicly accessible directory, not in whatever directory the servlet code or classes are in. I'd advise to set up and test the web app outside of any IDE. Then it should become clear quickly what the problem is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display JPEG file using Servlet