PDA

View Full Version : JSP & Servlet - can't fetch a file



Charlatan
05-04-2007, 07:58 AM
I've been tasked to do some servlet programming and sad to say, I'm just not that good at it. I've run into a sticky problem and am hoping that I can find a smart person who knows how I can accomplish this.

My task involves submitting a query to an SQL database, formatting the results, and displaying them in a nicely formatted web page. I've written a servlet using Java that runs on our JBOSS server (all firsts for me). It all seems pretty simple, and as of now I have it mostly working - there is one issue, however. One of the fields in the database is a string which is a jpeg filename. I'm supposed to display the file iteself in the web page. I can determine an absolute path for the file, which is on a shared volume, but I can't seem to get the image itself displayed in the webpage. For instance:

File name from database: prettyfile.jpg
Result from doing a lookup: /mnt/images/project/prettyfile.jpg

This file definitely exists, and I can examine it from my command shell in Linux. So the path is correct. Seemed to me that I need a way for the web page to suck the image from that file, and that's what I can't figure out.

From what I've been able to determine, I think I need to turn this file path into an URL in order to display it in the web page. So I've been playing around with the java URL class by getting the servlet context and calling getResource on this full path - but that's not working (getResource is returning null when I call it with the absolute path from above).

I know this has *got* to be possible but given that this is my first trip into Java land, servlet land, and JBOSS village, my knowledge is so incomplete that I'm reduced to trying all sorts of things that don't work - and I'm even sure if I need to do this in java, or if I need to modify the servlet so it can access this directory (by modifying the web.xml file?) or what!

Thanks in advance for any help/advice/tips.

Nick Walter
05-04-2007, 08:12 AM
This probably isn't a code problem, its a web server config issue.

You need to modify your application server/ web server so that the file to display is visible in the tree of served files. In a Red Hat Linux system for example, only files under /var/www are available to apache to serve up.

You should be able to just type in
http://yourappserver.name.here/some/path/prettyfile.jpg and see the dang file. Until you can to the point where a client can retrieve it directly you won't be able to give a link to it in your servlet.

There's more complicated answers involving embedding a link back to another servlet which would stream out the image data but I wouldn't mess with that until you are a bit more of a seasoned servlet programmer ;-)

bigdruid
05-04-2007, 10:06 AM
Yeah, that's good advice - once your web server can serve up that file directly, you can just do a server-side forward from your servlet to load that file, like so:

String url = "url to that file";
Request Dispatcher disp = request.getRequestDispatcher(url);
disp.forward(request, response);

or send a redirect (which is probably a better solution)

response.sendRedirect(url)

If you can't make your web server serve up the file, then you'll have to open the File (with a File object) create an input stream from it, get the output stream from the response, and write the bytes of the file out - it's not terribly hard. The tricky bit is you'll want to set the content type of the response to the appropriate mime type for whatever file you are sending down (like "image/jpeg"). For images, it's not critical (most browsers can auto-detect the image filetype based on the raw data) but if there are other types of files you'll need it.

Thrag
05-04-2007, 04:32 PM
[QUTOTE]One of the fields in the database is a string which is a jpeg filename. I'm supposed to display the file iteself in the web page. I can determine an absolute path for the file, which is on a shared volume, but I can't seem to get the image itself displayed in the webpage. For instance:

File name from database: prettyfile.jpg
Result from doing a lookup: /mnt/images/project/prettyfile.jpg[/QUOTE]

You're putting the wrong path on it. The path you want is not the absolute path on the file system, but the path relative to your web server doc root. Just make sure the image is stored under your web server's doc root somewhere and put the path relative to the doc root int the URL.

For example if the file is:

/myDir/something/project/images/file.jsp

And your doc root is:

/myDir/something/project

Then the path you want in the page is just /images/file.jsp

An image being displayed in a page generated from a jsp template is no different from an image being displayed in some static html file.