Search code examples
javajspjspinclude

How to determine which JSP pages are being rendered?


I'm working on a legacy application that is using simple JSPs that are nested using <jsp:include>.

No frameworks are being used - just JSP Servlets and filters.

Can anyone suggest a way to trace which JSP pages are being rendered?

Perhaps there's a log, or maybe a hook in the rendering engine (Jasper).


Solution

  • Create a Filter which listens on an url-pattern of *.jsp and the INCLUDE dispatcher only.

    <filter>
        <filter-name>includeFilter</filter-name>
        <filter-class>com.stackoverflow.q2242429.IncludeFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>includeFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>
    

    Get the parent page by HttpServletRequest#getServletPath() and the include page by HttpServletRequest#getAttribute() with key javax.servlet.include.servlet_path:

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException
    {
        HttpServletRequest httpreq = (HttpServletRequest) request;
        String parentPage = httpreq.getServletPath();
        String includePage = (String) httpreq.getAttribute("javax.servlet.include.servlet_path");
        // Log it here?
    
        chain.doFilter(request, response);
    }