Jack Dorsey
Jack Dorsey

Reputation: 285

Find which servlet a web request will hit

Inside a servlet Filter, is there a way to figure out which Servlet a particular request would eventually hit? I need to do Role based security checks based upon Servlets (not URL patterns) , in a filter (due to legacy reasons).

thanks

Upvotes: 2

Views: 97

Answers (2)

BalusC
BalusC

Reputation: 1108742

This information is not available by HttpServletRequest.

If you're already on Servlet 3.0 (Tomcat 7, Glassfish 3, JBoss AS 6, etc), then you can get information of all servlet registrations and mappings by ServletContext#getServletRegistrations():

for (Entry<String, ? extends ServletRegistration> entry = servletContext.getServletRegistrations().entrySet()) {
    String servletClassName = entry.getKey();
    Collection<String> urlPatterns = entry.getValue().getMappings();
    // ...
}

You could do the URL matching yourself based on the servlet mappings and the information as obtained by among others HttpServletRequest#getServletPath()

If you're not already on Servlet 3.0, then you need to parse the web.xml and collect all servlets and their URL patterns yourself, or to have a copy of it elsewhere. For manually parsing web.xml, the JAXB may come handy.

Upvotes: 1

Ramesh PVK
Ramesh PVK

Reputation: 15446

The HttpServletRequest.getServletPath() returns the part of the request URI that resulted in the servlet being invoked.

Upvotes: 0

Related Questions