elzapp elzapp - 1 year ago 99
Java Question

Get current filename in JSP

Is there a way to get which JSP is currently rendered, with JSTL or Struts (or without)? like _ _ file _ _ in Python and PHP?

Answer Source

Well ... yes ... in a way

String __jspName = this.getClass().getSimpleName().replaceAll("_", ".");

I'm using a JSP called pre.jsp for that which I include at the top of each JSP in my webapp:

<%@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 %> -->

Plus I put this at the end of each JSP:

<!-- END <%=__jspName %> --><% log.info("END JSP "+__jspName); %>

That gives me a consistend log. To make sure each JSP is "correct", I have a check in my build script which just looks for the two strings "/pre.jsp" and `END <%=__jspName.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download