File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet redirection

 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have in our application a need for our servlet to be redirected to a document and we dont want the URL of the document not to be displayed in the browser. sendRedirect does show the URL. Any ideas?
Thanks...
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this post.
 
Matt Horton
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed something interesting a few days ago using getServletContext().getRequestDispatcher( "/servletname" ).forward( request, response )
I forwarded from one servlet to another mapped servlet (e.g. from "/servlet1" which received the initial doGet(), to "/servlet2") and after validation, my attempt to use request dispatcher to forward BACK to "/servlet1" gave me the error "can not forward to same name URI" out of JRun's wrapper for request dispatcher.
Since /servlet1 and /servlet2 have different mappings, aren't they different URIs??? I received this bug against JRun 2.3.3; I had meant to test it against JRun 3.x but in the meantime I simply have /servlet2 do a sendRedirect( ".../servlet1" ).
Is this probably just a bug in my servlet container, or does it seem as if this exception is logical?
TIA.
[ March 02, 2002: Message edited by: matt horton ]
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want the URL of the document not to be displayed it means that you want it displayed, right?
 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i meant the url not to be displayed. now give me the solution.
 
Scott Ramsey
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you use RequestDispatcher.include()? (This can be retrieved either through the ServletRequest or the ServletContext, depending on whether the new resource is in the same webapp or not and whether it is public or not.)
This does not change the path elements in the response object, and hence should give no indication whatsoever to the user that the source URL of the response is different than the one requested.
I haven't tried this, but it is what I would try if I were you (and assuming I am understanding your problem correctly).
 
Matt Horton
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fyi, jrun 3.x fixed my problem
not that anybody was helping... but anyway....
 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
people, i am not redirecting from servlet to servlet, mind you. what i am actually pointing my servlet to is a document( a pdf, excelsheet etc).
[ March 04, 2002: Message edited by: vasu maj ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vasu, two people have given you potential solutions, but to re-iterate:

You have somewhat conflicting requirements. If you *need* the browser to be redirected, then you have no choice but to show *what* you are redirecting to in the browser's address bar. This is not up to you, it is the browser that is displaying the information.

Your remaining choices are foward() and include(). Since the resource is potentially a pdf file, your last remaining choice is forward().
web.xml
Place the servlet inside the /webapps/examples/WEB-INF/classes directory, and put redir.pdf at /webapps/examples

The browser bar when I make my request:
http://localhost:8080/examples/RedirectIt
The browser bar after the pdf loads:
http://localhost:8080/examples/RedirectIt
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are working with files you may also look at using a FileInputstream to read the file into the servlet and get an output stream from the request object.
 
Nicolas Brasseur
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should use the HttpResponse of a servlet to wrap your pdf document.
- "Hide" your file in the WEB-INF dir (I am a tomcat user)
- Use the getResourceAsStream() method of the ServletContext Object to get an InputStream Object corresponding to your file.
- Change the MIME type of your response by using the setContentType() method according to the MIME type of your file.
- "Copy" the inputStream content to the HttpResponse PrintWriter (a decoration will be needed)
... Am I right ???
Nicolas
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic