English 中文(简体)
如何利用吉大港山区应对办法调整用户方向
原标题:How to redirect users using HTTP response
  • 时间:2010-12-10 12:57:12
  •  标签:
  • java
  • struts

我有一个情景,即用户点击一家餐馆连接点(在某个地方搜查餐馆)。 我必须检查该地点是否固定。 如果无法确定,我想将他转往一个网页,使他能够确定地点,然后回去寻找由固定地点过滤的结果。 使用<代码>response.sendRedirect(url)将用户转至设定地点网页。 但是,我怎么能把回馈回URL(即,在安放地点之后,我想寄送用户的URL)?

我试图这样做:

response.sendRedirect("/location/set.html?action=asklocation&redirectUrl="+
            request.getRequestUri()+request.getQueryString());

但这只字塔和404个错误就显示出来;此外,浏览器中形成的ur也看好。

如果任何人能够解决问题......

最佳回答

看像你一样,在<代码>request.getRequestUri()和request.getQueryString(之间至少遗漏了“?”)。 您也可使用<条码>java.net.URLEncoder。

此外,在调整方向时,你需要预先选择环境途径:request.getContextPath(

类似于

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);

个人方面,即通过在届会期间储存并在确定地点后将其转交解决。

问题回答

我的第一份答复是删除关于您的URL的/,这种效果(载于您的法典):

response.sendRedirect("location/set.html?action=asklocation&redirectUrl="+
            request.getRequestUri()+request.getQueryString());

如果做上述工作,请在你抽签开始时添加<代码>request.getContextPath()等字样:

response.sendRedirect(request.getContextPath() + "/location/set.html?action=asklocation&redirectUrl="+request.getRequestUri()+request.getQueryString());

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签