PDF serving

Serving a file

I'm having trouble serving a PDF file from a server to the user.

I've used iText to generate a PDF file (by completing an AcroForm with info from a database) which now resides on an absolute path.

I've used BalusC's tutorial for fileserving.

Everything is working properly, except that clicking on downloading the file doesn't bring anything.

Here is my code:

The jsp from which you click to download the file has (and filename is valid and reading properly):

<h:outputLink value="file?name=#{ProjectsManager.filename}">

<h:outputText value="download #{ProjectsManager.filename}" />

</h:outputLink>

my web.xml mappings for the fileservlet:

<servlet>

<servlet-name>File Servlet</servlet-name>

<servlet-class>beans.FileServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>File Servlet</servlet-name>

<url-pattern>/file/*</url-pattern>

</servlet-mapping>

I've left the web.xml as is besides the class reference.

Are there any other properties I should modify or no? What is the purpose of the url-pattern?

Here is the beginning of my FileServlet.java:

publicclass FileServletextends HttpServlet{

protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException

{

String filePath ="/Users/Alex/iText/PDF/gen/";

String fileName = request.getParameter("name");

The error I'm getting is:

type Status report

message /06_proj_01/pages/design/file

description The requested resource (/06_proj_01/pages/design/file) is not available.

Thanks

[2134 byte] By [mon.goosea] at [2007-11-27 11:50:07]
# 1

The requested resource (/06_proj_01/pages/design/file) is not available.

Looking to the servlet mapping, the fileservlet is located at /06_proj_01/file. Fix the links.

BalusCa at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I'm not sure I undestand how URLs are mapped in JSF applications.

Are you saying I should change the <url-pattern> property of the servlet mapping? What path should I specify?

My FileServlet.java is with the rest of the java files in the beans package.

I can't find much on the internet that explains the function of the deployment descriptor, I'm sorry if my questions are so elementary.

Thanks again

mon.goosea at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Where does the generated <a href> link from the h:outputLink point to? Is this URL correct?

The error message says it is not. The resource behind this URL cannot be found (say, it's an ordinary 404 page not found error).

BalusCa at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

It points to: file?name=FILE_NAME.pdf

As I understand it, this should link to the FileServlet class which will take the name parameter and set it as the filename. Since that class already knows the absolute path for the directory where the file sits it should be able to create a new File instance.

This means I'm not correctly accessing the FileServlet class. The trouble is I don't know the URL of the FileServlet or how this class sits within the JSF structure at all. Any insight? I don't know what the correct URL is for the class, or whatever it is I need to access. If it helps, the class I need is sitting in the “beans” package.

Thanks again for your help

mon.goosea at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Oh, you're saying that the FileServlet is not definied in the web.xml? But you've posted the servlet mapping which looks fine.

And:

> It points to: file?name=FILE_NAME.pdf

Actually this was not my question. What is the complete request URI of this link?

BalusCa at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Sorry about that, where would I find the complete request URI?

That link points to:

http://localhost:8080/06_proj_01/faces/pages/design/

This is a more complete web.xml

<!-- servlet -->

<servlet>

<servlet-name>FacesServlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet>

<servlet-name>File Servlet</servlet-name>

<servlet-class>beans.FileServlet</servlet-class>

</servlet>

<!-- servlet-mapping -->

<servlet-mapping>

<servlet-name>FacesServlet</servlet-name>

<url-pattern>/faces/*</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>File Servlet</servlet-name>

<url-pattern>/file/*</url-pattern>

</servlet-mapping>

Thanks

mon.goosea at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

sorry, I meant

http://localhost:8080/06_proj_01/faces/pages/design/file?name=FILE_NAME.pdf

mon.goosea at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

I'll repeat myself:

> Looking to the servlet mapping, the fileservlet is

> located at /06_proj_01/file. Fix the links.

Try to manually invoke http://localhost:8080/06_proj_01/file?name=FILE_NAME.pdf

BalusCa at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Ok, I've realised I need

http://localhost:8080/06_proj_01/faces/file?name=complRequestURI.pdf

Thanks for guiding me through this. Now I just need to set up the links correctly

mon.goosea at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

Fine :)

BalusCa at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

This is posing more trouble than I thought it would.

How should I setup the link in the jsp,

no matter what I've tried setting it to, it won't point to the base /faces/ directory. It'll either use the /faces/pages/design/ path or just start file?name=after localhost:8080/

Thank you for your time

mon.goosea at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

No, don't point to the /faces/. This invokes the FacesServlet, not the FileServlet.

Just point to "/06_proj_01/file?name=FILE_NAME.pdf".

BalusCa at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13

Yes, but what should I set as the value of the outputLink?

So far nothing will produce the URL that I know works.

The only workaround I've found so far is to have it point to /faces/pages/design/file?name=

and this works if I have the servlet mapping as

/faces/pages/design/file

If I leave the web.xml as /file

the only URLs I can make appear from different values of the outputLink are

http://localhost:8080/06_proj_01/faces/pages/design/file?name=FILE.pdf -> doesn't work

http://localhost:8080/file?name=FILE.pdf -> doesn't work

I can never bring up the correct URL:

http://localhost:8080/06_proj_01/faces/file?name=FILE.pdf

mon.goosea at 2007-7-29 18:29:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...