0

I have a javax.servlet.Filter class that I would like to redirect urls. The problem is that when I call

httpResponse.sendRedirect("http://myurl/login.jsp")
return;

it doesn't redirect to there. I can see from the firebug network console that the request matches the correct url, but the page doesn't change and the current address bar url remains unchanged.

The request looks valid.

Request URL:http://myurl/login.jsp
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:localhost:7001
Referer:http://myurl/index.jsp
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
X-Requested-With:XMLHttpRequest
Response Headersview source
Content-Language:en-US
Content-Length:764
Content-Type:text/html; charset=ISO-8859-1
Date:Fri, 27 Apr 2012 16:49:55 GMT
X-Powered-By:Servlet/3.0 JSP/2.2

The response contains the contents of login.jsp

<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
...
</body>
</html>

I have tried using

request.getRequestDispatcher("/login.jsp").forward(request, response);

and that just doesn't work, as I see in Firebug it requests the url that I am currently on.

sworded
  • 2,419
  • 4
  • 31
  • 48

3 Answers3

1

Do not call chain.doFilter() when you send a redirect. Otherwise the filter will still continue the request on the initial URL and your server logs would be littered with IllegalStateException: response already committed exceptions everytime you tried to redirect.

See also this related answer: java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

after initializing the response stream (e.g. response.getWriter()) the redirect does not work:

for me the following code fails:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    log.info("incoming request :{}", request.getRequestURL());
    boolean failed;
    try {
         // do something
        failed = processGetRequest(request, response.getWriter());
    } catch (Exception e) {
        log.error("Error during collecting resource data -> redirect to root");
        failed = true;
    }

    if (failed) {
        response.setHeader("Location", "http://www.somewhere.de" );
        // 301 Moved Permanently
        // 307 Temporary Redirect
        response.setStatus( HttpServletResponse.SC_MOVED_PERMANENTLY);

    } else {
        response.setContentType("text/html;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
    }
}

but if I prepare the data in a temporary buffer (replace processGetRequest()) and open the writer later it works.....

kiar
  • 76
  • 2
0

I was redirecting a request from CometD, which doesn't actually send me anywhere. Redirects have to be done on GET requests.

sworded
  • 2,419
  • 4
  • 31
  • 48