• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another way to download

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I have a servlet that download pdf books.
this servlet takes the name of the book as a parameter like:
http://domain/download?name=ebook.pdf
now, I want to develope a servlet that has the following url pattern:
/*.pdf
so I can write:
http://domain/ebook.pdf
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
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the HttpServletRequest interface. getPathInfo() is probably what you're looking for.
 
Brahim Bakayoko
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing is stopping you of doing so.
The query string is not part of the URL pattern.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot.
but can you write some lines of code because I didn't understanding what you mean.
 
Mike Fuellbrandt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic