java 在 servlet 中获取请求 url 的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16501478/
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
Getting part of request url inside servlet
提问by JAVAGeek
I have an EmailVerification
Servlet mapped with /ev/*
url-pattern.
我有一个EmailVerification
用/ev/*
url-pattern映射的Servlet 。
http://example.com/ev/ce52320570
How can I get this ce52320570
part of the URL in my Servlet?
如何ce52320570
在我的 Servlet 中获取URL 的这一部分?
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String vid = ""; // Here I need to get the id from the URL
}
回答by acdcjunior
Considering a Servlet (called EmailVerification
) mapped to /ev/*
:
考虑EmailVerification
映射到的 Servlet(称为)/ev/*
:
Will the URL
http://example.com/ev/ce52320570
trigger theEmailVerification
servlet ?
URL 会
http://example.com/ev/ce52320570
触发EmailVerification
servlet 吗?
Yes. In Servlet versions 2.5 and 3.0 (maybe earlier), it'll get the subpath if you map it with *
, like /ev/*
, as you did.
是的。在 Servlet 版本 2.5 和 3.0(可能更早)中,如果您*
像 一样使用映射它,它将获得子路径/ev/*
。
How can I get this
ce52320570
part of the URLhttp://example.com/ev/ce52320570
?
如何获取
ce52320570
URL 的这一部分http://example.com/ev/ce52320570
?
request.getRequestURI()
will get you the requested URL as aString
, like/ev/ce52320570
.request.getPathInfo()
gets you (if exists) everything after/ev/
.- So in a request to
/ev/123
,getPathInfo()
would give you/123
. The same way, a request to/ev/some/other
,getPathInfo()
would give you/some/other
.
- So in a request to
request.getQueryString()
should be used if you need the query parameterspart of the URL.- Keep in mind both
getRequestURI()
andgetPathInfo()
give you only the pathrequested. If you need to obtain the query parameters, that is, those after the?
, like/ev/something?query1=value1&other=123
, onlyrequest.getQueryString()
would return thequery1=value1&other=123
part.
- Keep in mind both
request.getParameter(parameterName)
if you need the value of a specificquery parameter.- Resort to
.getParameterValues()
if it is a multivalued parameter
- Resort to
request.getRequestURI()
将为您提供请求的 URLString
,例如/ev/ce52320570
.request.getPathInfo()
让你(如果存在)之后的一切/ev/
。- 所以在一个请求中
/ev/123
,getPathInfo()
会给你/123
。用同样的方法,一个请求/ev/some/other
,getPathInfo()
会给你/some/other
。
- 所以在一个请求中
request.getQueryString()
如果您需要URL的查询参数部分,则应该使用它。- 记住两者
getRequestURI()
,只getPathInfo()
给你请求的路径。如果需要获取查询参数,即?
, like之后的/ev/something?query1=value1&other=123
,只request.getQueryString()
返回query1=value1&other=123
部分。
- 记住两者
request.getParameter(parameterName)
如果您需要特定查询参数的值。- 再打
.getParameterValues()
,如果它是一个多值参数
- 再打
More examples of the URL parts in the request here.
此处请求中的 URL 部分的更多示例。
回答by Mateusz
Use request.getRequestURI()
and remove what you don't need, i.e. request.getRequestURI().replace("/ev/");
使用request.getRequestURI()
并删除您不需要的东西,即request.getRequestURI().replace("/ev/");