3

Got a Java Filter which is responsible to intercept some endpoints.

In doFilter method, as follows:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;

How do I get the accessed method name?

For instance:

Given 2 Servlets followed by operation name:

LifeCycle
   create
   delete
SearchService
   findByName
   findById

When LifeCycle.create operation is called by a front end perspective, the filter intercepts it, however I couldn't know if the operation called was create or delete?

Are there some way to get the "create" operation name in Java Filter?

Thanks in advance.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
THIAGORC
  • 178
  • 1
  • 3
  • 11

3 Answers3

1

Filters are invoked by the web container when a request is made to the server (servlet or jsp). They are not called by Servlets or jsps.

You can see Filter's life-cycle in the image below:

enter image description here

For more see DOCUMENTATION

If you want to know which action is called from the front-end, then you can use a request parameter and then capture it from ServletRequest

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
1

I could get the operation name using:

((HttpServletRequest) request).getHeader("SOAPAction");
Pang
  • 9,564
  • 146
  • 81
  • 122
THIAGORC
  • 178
  • 1
  • 3
  • 11
-1

If you are searching for method names then you can try this piece of code:

    StackTraceElement[] st = Thread.currentThread().getStackTrace();
    String methodName = st[2].getMethodName();

You can further modify the index of st to get the chained caller methods. It is just a simple array of stack trace objects containing class and method names. Good Luck!

arnabkaycee
  • 1,634
  • 13
  • 26