4

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

2
  • Do you actually want the pattern or just the URL? Commented Mar 7, 2014 at 19:44
  • 1
    @SotiriosDelimanolis I actually want the pattern ;) Commented Mar 7, 2014 at 21:15

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

Comments

5

You can get the <url-pattern> registration for a servlet by

ServletContext servletContext = getServletContext();
ServletRegistration servletRegistration = servletContext.getServletRegistration();
java.util.Collection<java.lang.String> mappings = servletRegistration.getMappings()

and get the

final String path = getServletPath();

from request as Andreeas suggested and try to find out mapping by comparing string patterns


Javadocs

2 Comments

How I can get url pattern in servlet 2.5?? As servlet registration is not available in older version.
servletContext.getServletRegistration() does not exist, only servletContext.getServletRegistration(String servletName). In order to use that one, you can override GenericServlet.init(ServletConfig config) and get the mappings by executing Collection<String> mappings=getServletContext().getServletRegistration(config.getServletName()).getMappings(); in it. Also, servletRegistration.getMapping() does not exist. It should read servletRegistration.getMappings().
1

If you specifically want the URL mappings, there are a few ways, but they all require some information from the deployment.

For example, if you know the name of the Servlet, you can use ServletContext#getServletRegistration(String)

ServletContext context = ...;
Collection<String> mappings = context.getServletRegistration("servlet-name").getMappings();

If you don't know the name, you can get them all with ServletContext#getServletRegistrations()

Map<String, ? extends ServletRegistration> registrations = context.getServletRegistrations();

and try to find yours, maybe by comparing classes (your servlet class versus the class name from the ServletRegistration).

Note that you will still probably have to try matching your current request's URL to the Servlet's url mappings to be sure. You'd have to go to the Specification to find out how the mappings actually work.

1 Comment

How I can get url pattern in servlet 2.5?? As servlet registration is not available in older version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.