views:

83

answers:

4

I want to log every <jsp:include> tag.

Does the JavaServer Pages Standard Tag Library (JSTL) support logging and if so, how do I enable it?

+2  A: 

Not directly, but you could write your own tags. If you're sufficently crazy and industrious, you can write <thilo:include> tags that do <jsp:include> and call up a bit of Java code to do logging via log4j or such.

Carl Smotricz
I cannot change the JSP source. How do I map the new thilo:include to replace jsp:include?
Thilo
This is a very painful restriction, and to be honest I don't know how to overcome it. Sorry and good luck!
Carl Smotricz
axtavt has published an answer using filters which seems to be well regarded. You may be better off pursuing his answer rather than mine.
Carl Smotricz
axtavt's answer is the best way, but it requires servlet-2.4.
Thilo
+1  A: 

Find the actual Java code that implements this tag and check if there are any logging statements in it. If there are enable the class in your logging configuration. If there are no logging statements use AOP to define method interceptor which will log every invocation of Java implementation method. From a method interceptor you have access to invoked method arguments and if needed these can be logged as well.

Bear in mind that AOP may slow down the execution of your application. You may want to disable it in the production environment.

Boris Pavlović
A: 

There is no simple way to achieve this. My solution was a small file pre.jsp which contains:

<%@page import="org.apache.log4j.Logger"%>
<%
    String __jspName = this.getClass().getSimpleName().replaceAll("_", ".");

    Logger log = Logger.getLogger(this.getClass().getName());
    log.info("BEGIN JSP "+__jspName);
%>
<!-- BEGIN <%=__jspName %> -->

In every file, I'd then add this line at the beginning:

<%@ include file="/pre.jsp" %>

and this one at the end:

<!-- END <%=__jspName %> --><% log.info("END JSP "+__jspName); %>
Aaron Digulla
+5  A: 

You can implement a Filter and configure its mapping as following:

<filter>
    <filter-name>logging</filter-name>
    <filter-class>com.example.LoggingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>logging</filter-name>
    <url-pattern>*.jsp</url-pattern>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

This filter will intercept all RequestDispatcher.include() calls, including <jsp:include>. To get path to the included resource, use request.getAttribute("javax.servlet.include.servlet_path")

axtavt
Drat, I was going to post exactly the same answer. +1.
BalusC
+1 good solution, you beat me to it :)
skaffman
Note that this requires version 2.4 of the Servlet API.
Thilo
.. which has already been out for more than 6 years. I don't see any benefits of sticking to it or even any of the older versions which were drastically limited in JSP/EL possibilities.
BalusC