This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes another way to download Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "another way to download " Watch "another way to download " New topic

another way to download

Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3476

Hi all.
I have a servlet that download pdf books.
this servlet takes the name of the book as a parameter like:
now, I want to develope a servlet that has the following url pattern:
so I can write:
but the problem is, how the servlet could know the name of the book to download (the site could have many pdfs so I can't hard code the book's name in the servlet)
Mike Fuellbrandt

Joined: Jan 17, 2002
Posts: 14
Take a look at the HttpServletRequest interface. getPathInfo() is probably what you're looking for.
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
Nothing is stopping you of doing so.
The query string is not part of the URL pattern.

SCJP, SCWCD, SCBCD, IBM CSD WebSphere v5, <br />A+, MCP 2000 and 2000 server, CST, and few incompleted certification tracks.<br /> <br />Ivory Coast<br /> <br />Analyze your web Request/Response @ <a href="" target="_blank" rel="nofollow"></a> down for a while...
Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3476

Thanks alot.
but can you write some lines of code because I didn't understanding what you mean.
Mike Fuellbrandt

Joined: Jan 17, 2002
Posts: 14
There's two ways to attack this one I think...

First, set up a servlet-mapping for /download/* to point to your servlet.

When you call http://yourDomain/download/ebook.pdf, everything after the uri part 'download' will be returned by a call to request.getPathInfo() (i.e. /ebook.pdf). Strip off the preceding slash and you have your file name. The advantage here is you can include some directory structure to keep the pdf directory clean and organized.
i.e. http://yourDomain/download/manuals/java/user_guide.pdf -> %PDF_ROOT%/manuals/java/user_guide.pdf

The disadvantage would be that you are exposing the directory structure which may be a security hazard for you.

The second way would be to map *.pdf in the servlet-mapping to your servlet. Then you can call http://yourDomain/downloads/ebook.pdf and request.getServletPath() will return /downloads/ebook.pdf.
I agree. Here's the link:
subject: another way to download
Similar Threads
Servlet / JSP Tutorial
Preparation for SCBCD from basics
SCBCD study books/guide/mock exam recommendations
Java FX Script
Books on JAVAFX?