Alternative to requestDestroyed in ServletRequestListener

Viewed 247

I have multiple servlets in my web app. And I want to perform a specific action each time a request comes to MyServlet (only) and not when request is for any other servlet. Overriding destroy isn't going to solve this. If I write a custom ServletRequestListener, that action will be performed for all requests, if I put that action in requestDestroyed method, irrespective of which servlet in my web app is being called.

So, does writing requestDestroyed method as a part of servlet itself like below solves this problem ? Or is there any other/better way?

public class MyServlet implements HttpServlet {
    public void init(ServletConfig config) throws ServletException {
        //init stuff
    }

    public void service(ServletRequest req, ServletResponse res)
            throws ServletException {
        try {
            //do some stuff
        } finally {
            requestDestroyed(request);
        }
    }

    private void requestDestroyed(HttpServletRequest request) {
        //some clean up stuff only for this particular servlet
    }
}
0 Answers
Related