问题描述
我有 EmailVerification
使用 / ev / *
url-pattern映射的Servlet。
I have an EmailVerification
Servlet mapped with /ev/*
url-pattern.
http://example.com/ev/ce52320570
如何在我的Servlet中获取此 ce52320570
部分URL?
How can I get this ce52320570
part of the URL in my Servlet?
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String vid = ""; // Here I need to get the id from the URL
}
推荐答案
考虑将Servlet(称为 EmailVerification
)映射到 / ev / *
:
Considering a Servlet (called EmailVerification
) mapped to /ev/*
:
是。在Servlet版本2.5和3.0(可能更早)中,如果您使用 *
映射它,它将获得子路径,如 / ev / *
,正如你所做的那样。
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.
-
会以
字符串
为您提供所请求的网址,例如/ ev / ce52320570
。request.getRequestURI()
will get you the requested URL as aString
, like/ev/ce52320570
.在
/ ev /之后获取(如果存在)所有内容
。- 所以在请求
/ ev / 123
,getPathInfo()
会给你/ 123
。同样的方式,对/ ev / some / other
,getPathInfo()
的请求会给你/ some / other
。
- 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
.
。
request.getQueryString()
should be used if you need the query parameters part of the URL.- 请记住
getRequestURI()
和getPathInfo()
只为您提供 路径 。如果您需要获取查询参数,即?
之后的那些参数,如/ ev / something?query1 = value1& other = 123
,只有request.getQueryString()
将返回query1 = value1& other = 123
part。
- Keep in mind both
getRequestURI()
andgetPathInfo()
give you only the path requested. 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.
如果您需要 特定 查询参数的值。
- 求助于如果是多值参数
请求中URL部分的更多示例。
More examples of the URL parts in the request here.
这篇关于在servlet中获取请求url的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- 所以在请求