Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet: How to get own URL pattern?

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)?

like image 219
Gili Avatar asked Dec 08 '22 09:12

Gili


1 Answers

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()

like image 50
Andres Avatar answered Dec 31 '22 15:12

Andres