14

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
}
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
JAVAGeek
  • 2,674
  • 8
  • 32
  • 52
  • If you want *path parameters* you will have to handle them on yourself, basing off `HttpServletRequest` methods. If you could substitute them for *query parameters* everything would be easier. – skuntsel May 11 '13 at 20:51
  • I have posted an answer it may be better while it will return the last part only also without containing a slash... – Mohammed Alaghbari May 11 '13 at 21:57
  • @acdcjunior I need to take extra care of url's like `http://xyz.com/ev/ce52320570/` - _having an extra `/` in the end_ . it looks a bit of extra work to split and replace stuff. Do you have any better way for this ? – JAVAGeek May 12 '13 at 21:29
  • From `http://xyz.com/ev/ce52320570/`, `request.getPathInfo()` gives you the string `/ce52320570/`. If you want to eliminate the `"/"`s, just call [**`String#replace()`**](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html): `String vid = request.getPathInfo().replace("/", "");`. It will give you exactly `ce52320570`. And this is file system independent. – acdcjunior May 12 '13 at 21:41

2 Answers2

32

Considering a Servlet (called EmailVerification) mapped to /ev/*:

Will the URL http://example.com/ev/ce52320570 trigger the 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.

How can I get this ce52320570 part of the URL http://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/.

    • 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.

    • Keep in mind both 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.


More examples of the URL parts in the request here.

Community
  • 1
  • 1
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • is their any option other then splitting `/ev/ce52320570` after last `/` to get the id – JAVAGeek May 11 '13 at 20:52
  • Yes, `request.getPathInfo()` gets (if exists) you everything after `/ev/`. – acdcjunior May 11 '13 at 20:58
  • 2
    @acdcjunior In the last part `request.getParameter(paramName)` is more applicable than `requedt.getQueryString()`, as it allows to get the sent value in one go. – skuntsel May 12 '13 at 06:41
1

Use request.getRequestURI() and remove what you don't need, i.e. request.getRequestURI().replace("/ev/");

Mateusz
  • 3,038
  • 4
  • 27
  • 41