Is there a way to get which JSP is currently rendered, with JSTL or Struts (or without)? like _ _ file _ _ in Python and PHP?
4 Answers
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`.
Note: There are many characters which are allowed in file names but not in Java class names. If you use them, your class names might look weird. If that's the case, I suggest to create a static helper function which converts class names to File names and call that, i.e.
String __jspName = MyJspUtils.getFileName(this.getClass());
Each JSP compiler has it's own rules; here is one example: http://itdoc.hitachi.co.jp/manuals/3020/30203Y0510e/EY050044.HTM
Kudos go to Marcus Junius Brutus for pointing that out.

- 1
- 1

- 321,842
- 108
- 597
- 820
-
Yeah. that was exactly the usecase I had... I guess it'll do – elzapp May 14 '09 at 13:39
-
Notice that this only works for Java 1.5 or later. Otherwise it reports "Cannot resolve symbol" error – Scott Chu Apr 16 '14 at 05:48
-
@ScottChu: For Java 1.4, use `getClass().getName()` instead. – Aaron Digulla Apr 16 '14 at 07:11
-
I don't think this will work for includes. This will only print out the name of the initial JSP. – Zeki Oct 06 '14 at 14:00
-
@Zeki: That depends on how includes are implemented. In our app server, an include would generate code to create a new instance. If all the code is inlined, then it breaks. – Aaron Digulla Oct 06 '14 at 14:26
-
2This will not work if there are underscores or hyphens in your JSP filename. E.g. if you have JSPs like `foo-1.jsp` or `foo_1.jsp`. The JSP filename to classname conversion (e.g. http://itdoc.hitachi.co.jp/manuals/3020/30203Y0510e/EY050044.HTM) require you to take into account escape sequences like `_005f` (for underscores) etc. Up-voted, but it does seem brittle unless you enforce JSP filenaming conventions that ensure that such characters won't appear. – Marcus Junius Brutus Nov 05 '16 at 20:32
-
@MarcusJuniusBrutus Thanks, I've improved my answer. – Aaron Digulla Nov 09 '16 at 13:07
-
This has problem in a jsp that is included by other jsp. A true experiment: I use this in 'header.jsp', and it's included by fp_index.jsp running under Tomcat 6, the log dumps 'fp.005findex.jsp', instead of 'header.jsp'. Exactly like what Zeki said in comment. – Scott Chu Jul 12 '17 at 08:07
-
@ScottChu You're using Tomcat 6 in 2017? You must love taking security risks. That said, my answer to Zeki still holds: "**That depends on how includes are implemented**". I also explain this in my answer. You can try to work around this by using a taglib, a different JSP container, by not using includes or by abandoning JSP. That's about it. – Aaron Digulla Sep 04 '17 at 11:50
I succeeded using JSTL as following :
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<!-- <c:out value="${pageScope['javax.servlet.jsp.jspPage']}"></c:out> -->
...
And now, you should see as an HTML comment the name of the servlet produced by the container to render your JSP file, which name is very close to the JSP source file.

- 11
- 3
This is a simple copy-paste solution:
<%=this.getClass().getSimpleName().replaceFirst("_jsp","")%>

- 13,196
- 5
- 87
- 72
The more convenient way is to use: <%= request.getRequestURI() %>
<%= request.getRequestURI() %> For example, in all of my jsp files, I always do put this line:
Rendering JSP File: '<%= request.getRequestURI() %>'
This puts a comented html line in to the rendered html. This way one cannot see it in the browser, but for debugging purposes, I can always see it inf I do "View source".

- 995
- 2
- 14
- 25
-
1But that only shows the requested URI, not the rendered jsp, if it is coming from an include or rendered from a servlet – elzapp Dec 05 '12 at 14:21