1
votes

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!

1
Can you elaborate what exactly your problem is? According to your filter URL pattern the filter should be called for every request, even for those without an explicit servlet mapping. You should restrict the filter URL pattern if this is not what you want. - vanje

1 Answers

2
votes

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"})