aspose file tools*
The moose likes Servlets and the fly likes Problem displaying pdf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem displaying pdf" Watch "Problem displaying pdf" New topic
Author

Problem displaying pdf

ALi Taimur
Greenhorn

Joined: Jul 25, 2006
Posts: 1
Hello,

I need to display some pdf files which are sitting in a sub-folder inside WEB-INF. The path to the pdf is passed as a parameter from a jsp. The problem I am having is that instead of looking at the context path of my web-app, servlet is trying to load the pdf from the absolute path. For example, if the I pass the path /WEB-INF/HTMLHelp/common/sample.pdf, it goes to my hard drive and tries to look up
c: \WEB-INF\HTMLHelp\common\sample.pdf instead of going to the root of my application. Please let me know what I am doing wrong. Thanks in advance
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67


String filename =(String)request.getParameter("url");
...
File pdf= new File(filename);


It's doing exactly what you are telling it to do. What would make you think that the File object would somehow magically detect that you want to make the filename you pass to it relative to another folder?

You need to tell the File object the complete path to the file you want to open.

Hint: check out the ServletContext.getRealPath() method.
[ July 25, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

P.S.

String filename =(String)request.getParameter("url");


Why are you bothering to cast a String to a String?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

That's a common mistake I find myself making every now and again. I forget that getParameter returns a String and not an Object.


GenRocket - Experts at Building Test Data
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you're storing the files under WEB-INF (or anwhere within your webapp's directory structure) you shouldn't be using absolute file paths.
Look at ServletContext.getResourceAsStream.

Depending on how your app is deployed, getRealPath may or may not return a null value.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem displaying pdf