Reputation: 31
I am uploading few images with an upload functionality written in java. As soon as the upload is in progress there is a gif to show the progress bar. Now when there is the session timeout I am redirecting to the login page.
if (StringUtils.isNotBlank(request.getQueryString())) {
queryString = request.getServletPath() + "?" + request.getQueryString();
} else {
queryString = request.getServletPath();
}
LOGGER.debug("Query String:" + queryString);
if (!StringUtils.equalsIgnoreCase(StringUtils.substringAfterLast(queryString, ApplicationConstants.DOT),
ApplicationConstants.JSON)) {
request.getSession().setAttribute(ApplicationConstants.QUERY_STRING, queryString);
}
try {
LOGGER.error("In navigateToLandingPage. Redirecting to Failure View:" + this.getFailureView());
response.sendRedirect(request.getContextPath() + "/" + this.getFailureView());
// response.setHeader("Refresh", "5; URL=http://XXXX/login.htm");
} catch (IOException e) {
throw new BusinessException("Exception in redirecting", "-1", this.getClass().getCanonicalName(), e);
}
When I am analyzing the network in browser i can see the response with 302 status and also the location has the intended URL but some how the response in not getting processed and stuck at the progress bar screen. If i manually refresh the stuck page i am directed on the login page i am trying to add response.setHeader("Refresh", "5; URL=http://lXXXXX/login.htm"); below the redirect but no luck. Can anyone please help me around with this.
There is a workaround where instead of sending 302 i am sending 901 which i am checking at js and directing to login which works fine but i intend to use redirect.
Upvotes: 1
Views: 148
Reputation: 1109542
So, the request was fired asynchronously using JS instead of synchronously using web browser? In other words, you were trying to redirect an ajax request?
If you're sending a redirect as response to an ajax request, then XMLHttpRequest
will by default just follow that redirect. I.e. it will re-send the ajax request to that URL. This is obviously not what you intented. You want a synchronous redirect, not an asynchronous one.
The canonical approach is to just instruct the ajax engine in some way that it needs to perform a window.location
call on the given URL. If you're using JSON or XML as data format, then you can just add a special instruction which your ajax engine can then check on.
E.g. in case of JSON:
response.getWriter().printf("{redirect:\"%s\"}", url);
var response = fireAjaxRequestAndGetResponseAsJsonSomehow();
if (response.redirect) {
window.location = response.redirect;
return;
}
// Usual process continues here.
Upvotes: 1