0

I wish to redirect my code to the same page with an extra parameter in the url if a few conditions are met. How would i do so?

I tried using response.setHeader but it is not not working.

if(request.getParameter("lang")==null && UserProfileHelper.readCookie(request,"bha_code")!=null){
    langCode=UserProfileHelper.readCookie(request,"bha_code");
    if(langCode!=null && langCode!="en")
    {
       String url=CafeUtils.getCurrentURLNonUFT8(request);
       String[] urlNew = url.split(fullLailaURL);
       url=fullLailaURL+"/"+langCode+urlNew[1];
       response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
       response.setHeader("Refresh", url);
    }
}

I want the url in the browser to reflect the new one.

SC_MOVED_TEMPORARILY= 302

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Aman
  • 85
  • 9
  • check if `RequestDispatcher` can achieve this task. – Vishrant Dec 01 '15 at 12:58
  • @Vishrant: Nope, OP clearly want to send a redirect, not a forward. OP explicitly used the term "redirect" and explicitly stated *"I want the url in the browser to reflect the new one."*. In case you have no idea what that means, head to http://stackoverflow.com/a/2048640 – BalusC Dec 01 '15 at 13:33

1 Answers1

0

This is wrong.

response.setHeader("Refresh", url);

The HTTP spec clearly says you must use the Location header field.

response.setHeader("Location", url);

It can even be done simpler. Instead of the two lines,

response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", url);

you can just use the standard HttpServletResponse#sendRedirect() method.

response.sendRedirect(url);

See also its javadoc (emphasis mine):

Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. The buffer will be replaced with the data set by this method. Calling this method sets the status code to SC_FOUND 302 (Found).

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