This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlet redirection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet redirection" Watch "servlet redirection" New topic
Author

servlet redirection

vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
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...


What a wonderful world!
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Check out this post.


Matthew Phillips
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
If you don't want the URL of the document not to be displayed it means that you want it displayed, right?


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
i meant the url not to be displayed. now give me the solution.
Scott Ramsey
Greenhorn

Joined: Jun 28, 2001
Posts: 16
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).


Scott<br />-----------------<br />SCJP, SCJD, SCWCD<br />XML, UML, WLS6.0
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
fyi, jrun 3.x fixed my problem
not that anybody was helping... but anyway....
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Mar 09, 2001
Posts: 2676
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

Joined: Nov 09, 2001
Posts: 45
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


<a href="http://www.javablackbelt.com/?utm_source=javaranch&utm_medium=signature&utm_campaign=Forum%2BBuzz" target="_blank" rel="nofollow">BlackBeltFactory Communtiy</a> founder - Building better developers<br /><b>Free</b> courses and mock exams
 
wood burning stoves
 
subject: servlet redirection