aspose file tools*
The moose likes Servlets and the fly likes Redirect page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Redirect page" Watch "Redirect page" New topic
Author

Redirect page

Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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

Joined: Sep 29, 2001
Posts: 124
Yeah I found what I was doing wrong. Thanks for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Redirect page