This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes sendRedirect problem!help me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "sendRedirect problem!help me" Watch "sendRedirect problem!help me" New topic

sendRedirect problem!help me

Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41

First,I want to show "hello" on browser,
Next,redirect to
but,the browser shows only "hello",don't redirect to
If del out.flush(),browser can redirect to don't show "hello".
who can tell me, why?

Val SCJP2 1.2<br />====>SCJD
suresh thallam

Joined: Sep 08, 2002
Posts: 2
replace your code with this one
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<html><body>Hello!</body><script>window.location.href=''</script></html>");
Please let me know, if still have problems
Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
you can't call "flush" before calling sendRedirect.
as a general rule, flush means your response is over, nothing more. no more changes in status code, headers, or response-body. Otherwise, IllegalStateException. Tomcat doesn't give your that exception, because it is lenient. It just ignores the output after flush.

Kyle Tang<br />SCJP 91<br />SCWCD 96<br />SCBCD 95
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
Maha Annadurai
Ranch Hand

Joined: Oct 27, 2002
Posts: 87
This line of code
after out.flush() does produce the IllegalStateException in Tomcat also. Check your log file (C:\jakarta-tomcat-4.0.3\logs\localhost_log_date.log) The exception is logged in there.
Since the response is already commited , there is no way to send this exception to browser again. This is the reason you do not see any exception displayed in browser.
If you try to make a try ... catch block then you can realize that the IllegalStateException is reaised indeed.

Maha Anna
[ November 30, 2002: Message edited by: Maha Annadurai ]
I agree. Here's the link:
subject: sendRedirect problem!help me
It's not a secret anymore!