Java 如何使用 HTTP 响应重定向用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4409019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect users using HTTP response
提问by Shwetanka
I have a scenario where the user clicks on a 'restaurant' link (for searching restaurants in a particular locality). I have to check whether the location is set or not. If it is not set, I want to redirect him to a page that allows him to set the location, and, then, go back to search results filtered by the set location. I'm using response.sendRedirect(url)
to redirect the user to the setting location page. But, how can I send the redirect back URL (i.e., the URL where I want to send the user after the location is set)?
我有一个场景,用户点击“餐厅”链接(用于搜索特定地点的餐厅)。我必须检查是否设置了位置。如果没有设置,我想将他重定向到一个允许他设置位置的页面,然后返回到按设置位置过滤的搜索结果。我正在使用response.sendRedirect(url)
将用户重定向到设置位置页面。但是,如何发送重定向回 URL(即,在设置位置后我想将用户发送到的 URL)?
I tried this:
我试过这个:
response.sendRedirect("/location/set.html?action=asklocation&redirectUrl="+
request.getRequestUri()+request.getQueryString());
but this isn't working and 404 error is shown; also, the url formed in the browser doesn't look good.
但这不起作用,并显示 404 错误;此外,在浏览器中形成的 url 看起来不太好。
Please, if anyone could solve the problem ...
拜托,如果有人能解决这个问题......
采纳答案by Christoffer Hammarstr?m
Looks like you're missing at least a "?" between request.getRequestUri()
and request.getQueryString()
. You should url-encode the parameter as well, which you can use java.net.URLEncoder
for.
看起来您至少缺少一个“?” request.getRequestUri()
和之间request.getQueryString()
。您还应该对参数进行 url 编码,您可以使用它java.net.URLEncoder
。
Also, when doing redirects you need to prepend the context path: request.getContextPath()
.
此外,在进行重定向时,您需要预先添加上下文路径:request.getContextPath()
.
Something like
就像是
String secondRedirectUrl = request.getRequestUri()+"?"+request.getQueryString();
String encodedSecondRedirectUrl = URLEncoder.encode(secondRedirectUrl, serverUrlEncodingPreferablyUTF8);
String firstRedirectUrl = request.getContextPath()+"/location/set.html?action=asklocation&redirectUrl="+encodedSecondRedirectUrl;
response.sendRedirect(firstRedirectUrl);
Personally, i'd rather solve the problem by storing a RequestDispatcher
in the session and forwarding to it after the location has been set.
就个人而言,我宁愿通过RequestDispatcher
在会话中存储 a并在设置位置后转发给它来解决问题。
回答by Buhake Sindi
My first response will be to remove the /
on your URL, something of this effect (to your code):
我的第一反应将是删除/
您的 URL 上的 ,这种效果(对您的代码):
response.sendRedirect("location/set.html?action=asklocation&redirectUrl="+
request.getRequestUri()+request.getQueryString());
If that doesn't work, add request.getContextPath()
at the beginning of your url string, like so:
如果这不起作用,请request.getContextPath()
在 url 字符串的开头添加,如下所示:
response.sendRedirect(request.getContextPath() + "/location/set.html?action=asklocation&redirectUrl="+request.getRequestUri()+request.getQueryString());
The Javadocstates:
该Javadoc中指出:
If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
如果位置是相对的,没有前导“/”,则容器将其解释为相对于当前请求 URI。如果位置与前导“/”相关,则容器将其解释为相对于 servlet 容器根。