File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Redirect page

 
Chris Stewart
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an html form with the action being forwarded to my servlet. After the servlet actions execute I would like the user to be sent back to the index.html page. Can anyone help here?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two primary means to do this:
If you want the browser to create a new request in which to display the page, you would redirect to index.html using the sendRedirect() method of the response.
If you want to display the page in the context of the same request (which you would want to do if the page depends upon anything your servlet has placed into request -- unlikely in this case since your target page is an html page and not a JSP), you would forward using a RequestDispatcher instance.
hth,
bear
[ March 05, 2002: Message edited by: Bear Bibeault ]
[ March 05, 2002: Message edited by: Bear Bibeault ]
 
Chris Stewart
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently, I'm trying

But it is throwing "response already committed". I'm pretty confused as to these two redirect methods.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris, that error message means that output has already been comitted to the response. Once that happens, you can no longer redirect. Your code should decide whether a redirect is necessary prior to producing any output.
If you think about it for a minute this makes sense. If you have written output to the response AND tell the browser to display a file such as your index.html, which should it show?
If your intent is to emit some output, and THEN append some output from an html or JSP file to that, you would use neither a redirect nor a forward, but rather an include (see getRequestDispatcher() on the request).
hth,
bear
 
Chris Stewart
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I found what I was doing wrong. Thanks for your help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic