Can a servlet or filter look up its own URL pattern?
Meaning, if I bind some servlet or filter to /first/*
and /second/*
and a request comes in, can I find out which of the two patterns triggered it?
Even if a servlet is bound only to one pattern, is there a way to look it up from inside the servlet (instead of hard-coding a value)?
This method on the HttpServletRequest class will help you. You'll get an instance of HttpServletRequest on any of the Servlet methods invoked by a HTTP Request.
getServletPath
java.lang.String getServletPath() Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.
Take a look at this:
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getServletPath()
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