支持具有相同 servlet 的两个 URI
目前,我们网站上的所有链接都设置为 /xxx/UseCase
。
我们现在希望接受带有前缀 /yyy
的传入请求,例如 /yyy/xxx/UseCase
。因此,我们将有一个 servlet 接受 /yyy/xxx/UseCase
和 /xxx/UseCase
上的请求。
我们怎样才能做到这一点?
这里最大的挑战是:许多页面使用绝对路径重定向到其他 URI。即:Patients
servlet 重定向到/xxx/Insurance
。问题是,如果您位于 /yyy/xxx/Patients
,您现在不应该被重定向到 /xxx/Insurance
- 您应该被重定向到 /yyy /xxx/保险
Currently we have all links on our site set up as /xxx/UseCase
.
We would like to now accept incoming requests with the prefix /yyy
such as /yyy/xxx/UseCase
. So we would have one servlet that accepts requests on both /yyy/xxx/UseCase
and /xxx/UseCase
.
How can we do this?
Biggest challenge here being: A number of pages redirect to other URI's using the absolute path. Ie: the Patients
servlet redirects to /xxx/Insurance
. Issue is if you're at /yyy/xxx/Patients
you shouldn't now get redirected to /xxx/Insurance
- you should get redirected to /yyy/xxx/Insurance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 servlet 映射到多个 URL 模式:
或者当您已经使用 Servlet 3.0 时:
如果需要,您可以使用
/yyy/xxx/*
而不是/yyy/*
。或者,如果您打算不更改 servlet 映射,您也可以创建一个
Filter
,其中将/yyy/xxx/*
上的请求转发到/xxx/*
。像这样的事情(省略基本检查):如果您打算将浏览器地址栏中的 URL 从
/yyy/xxx/*
更改为/xxx/*
,那么您需要使用HttpServletResponse#sendRedirect()
而不是RequestDispatcher#forward()
:You can just map a servlet on more than one URL pattern:
Or when you're already on Servlet 3.0:
You can if necessary use
/yyy/xxx/*
instead of/yyy/*
.Or if you intend to untouch the servlet mapping, you could also create a
Filter
which forwards requests on/yyy/xxx/*
to/xxx/*
. Something like this (basic checks omitted):If you intend to change the URL in the browser address bar from
/yyy/xxx/*
to/xxx/*
, then you need to useHttpServletResponse#sendRedirect()
instead ofRequestDispatcher#forward()
:我认为问题不在于您的 servlet 映射。我认为这与你如何进行重定向有关。尝试一下您的重定向逻辑...
这样您就不必弄乱 if 语句,因为它应该在两种情况下都有效。
I don't think the problem is with your servlet mapping. I think it is with how you're doing your redirects. Try this for your redirect logic...
This way you don't have to mess with if statements as it should work in both situations.