File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes URL Redirection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL Redirection" Watch "URL Redirection" New topic

URL Redirection


Joined: Feb 13, 2000
Posts: 21
Hello ,
I have the foolowing problem:
I have developed an applet that communicates with a servlet. The applet
sends some objects to the servlet. After doing some processing I want to
redirect to some other site from within my servlet.
I tried the following methods.
1. HttpResponse.sendRedirect(URL);
2. construct the printWriter
PrintWriter out= HttpResponse.getWriter();
and then construct an HTML response as
out.println("<html><head>....<body> ...</html>");
Unfortunately, none of these methods work . I am not getting any compilation
errors. also note that the above two methods work if the user interface is a form and fails
if the front end is an applet.
Please suggest an alternative.
Thanks in Advance
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I had this problem before.
In my experience, HttpResponse.sendRedirect(URL) won't work after the servlet write anything to the browser (I don't know why). What I did at last was using JavaScript to redirect the browser (let it delay 1~2s).
sean zang
Ranch Hand

Joined: Apr 22, 2000
Posts: 33
What servlet Container(java Based Web server) Do you use?
If you use Jakarta -tomcat, the response.sendRedirect(URL) works
prety well! The problem is that you can not see out.println("somthing") if you response.sendRedirect(URl) statelmet follows the out.prinltln() because redreicted URL imediately occupy the browser! I use alots sendRedirect in my
project. it works fine. try to use response forward method !
I agree. Here's the link:
subject: URL Redirection
It's not a secret anymore!