HttpServletResponse sendRedirect permanent

前端 未结 2 418
你的背包
你的背包 2020-12-01 07:29

This will redirect a request with a temporary 302 HTTP status code:

HttpServletResponse response;
response.sendRedirect(\"http://somewhere\");


        
相关标签:
2条回答
  • 2020-12-01 07:36

    I used the following code, but didn't worked for me.

    String newURL = res.encodeRedirectURL("...");
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.sendRedirect(newURL);
    

    then I tried this piece of code it worked for me

    String newURL = res.encodeRedirectURL("...");
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", newURL);
    

    this worked for me, I had the same issue

    how to set status to 301 while redirecting

    0 讨论(0)
  • 2020-12-01 07:43

    You need to set the response status and the Location header manually.

    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", "http://somewhere/");
    

    Setting the status before sendRedirect() won't work as sendRedirect() would overridde it to SC_FOUND afterwards.

    0 讨论(0)
提交回复
热议问题