I have an EmailVerification
Servlet mapped with /ev/*
url-pattern.
http://example.com/ev/ce52320570
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
}
By design, getRequestURL() gives you the full URL, missing only the query string. . getScheme() will give you "https" if it was a https://domain request.
The getRequestURL() omits the port when it is 80 while the scheme is http , or when it is 443 while the scheme is https . So, just use getRequestURL() if all you want is obtaining the entire URL. This does however not include the GET query string.
A request URL consists of an HTTP method, a base URL, and a resource URI. The request header also includes parameters such as the content type and authorization information.
The function getRequestURI() returns the complete requested URI. This includes the deployment folder and servlet-mapping string. It will also return all extra path information.
Considering a Servlet (called EmailVerification
) mapped to /ev/*
:
Will the URL
http://example.com/ev/ce52320570
trigger theEmailVerification
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.
How can I get this
ce52320570
part of the URLhttp://example.com/ev/ce52320570
?
request.getRequestURI()
will get you the requested URL as a String
, like /ev/ce52320570
.
request.getPathInfo()
gets you (if exists) everything after /ev/
.
/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()
and getPathInfo()
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
, only request.getQueryString()
would return the query1=value1&other=123
part.request.getParameter(parameterName)
if you need the value of a specific query parameter.
.getParameterValues()
if it is a multivalued parameterMore examples of the URL parts in the request here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With