user1386966
user1386966

Reputation: 3392

How to check if url has valid mapping when using java Filter on ServletRequests

I'm using Filter and saving a mapping with all urls (and number of times each url was called).

@WebFilter(filterName = "SessionFilter", urlPatterns = {"/*"})
public class SessionFilter implements Filter {...}

I have this : @WebServlet(/test/aaa) so I expect the filter to get the request and forward it to my service. The issue is that if I send some post fake request like : "testing/lalalal" - it passes the filter and then I insert it to my map (which should not happen if I don't have valid mapping for it)

I tried using urlValidator but it didn't seem to help. I also tried to find if I get some error (404) but don't know where to look.

can anyone advise?

Thanks!

Upvotes: 1

Views: 962

Answers (1)

BalusC
BalusC

Reputation: 1108692

Instead of mapping the filter to an overly generic URL pattern, map it to URLs and/or servlets of actual interest. Below example maps it to a specific servlet.

@WebFilter(filterName = "sessionFilter", servletNames = {"yourServlet"})

Don't forget to give your servlet a name.

@WebServlet(servletName = "yourServlet", urlPatterns = {"/test/aaa"})

Upvotes: 2

Related Questions