Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

sendRedirect problem!help me

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First,I want to show "hello" on browser,
Next,redirect to www.yahoo.com
but,the browser shows only "hello",don't redirect to www.yahoo.com.
If del out.flush(),browser can redirect to www.yahoo.com.But don't show "hello".
who can tell me, why?
thanks.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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='http://www.yahoo.com'</script></html>");
Please let me know, if still have problems
Thanks
Suresh
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line of code
response.sendRedirect("http://www.yahoo.com";);
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.

Regards,
Maha Anna
[ November 30, 2002: Message edited by: Maha Annadurai ]
 
He's giving us the slip! Quick! Grab this tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic