-1

Due to lot of forward request, it is hard to identify the current showing jsp file. Is it possible to get the current jsp file path in war? How to achieve?

For an example: URL: http://localhost:8080/controller/action

File Path: /view/dev/test.jsp

<%= getServletContext().getRealPath("/") %> 

The above statement doesn't work, it show C:\Projects\test\build\web\ instead of C:\Projects\test\build\web\view\dev\test.jsp

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
Error 404
  • 107
  • 2
  • 13
  • 1
    @RobertMoskal This SO question doesn't have any accepted answers. Typically, I would only vote to close with a "solid" SO question as reference. – Tim Biegeleisen Jun 17 '15 at 01:34
  • Why do you need a physical disk file system's path corresponding to the current URL? `getRealPath()` has no single sensible usage, by the way (It should not even have been introduced). – Tiny Jun 17 '15 at 02:53
  • Please see http://stackoverflow.com/questions/17400077/servlet-real-path – diufanman Jun 17 '15 at 03:02
  • @Tiny Instead of full path, I would like to have file path in Test.war. – Error 404 Jun 17 '15 at 04:45
  • getRealPath won't work, as it needs to be provided a path in the first place, which he says he doesn't have. – Will Hartung Jun 17 '15 at 05:03

1 Answers1

0

This will be implementation dependent, but since many implementation use Jasper, it may well be more portable than you think. Of course, portability may not be an issue.

But the simple case is that you take the full qualified class name of "this" from within the JSP, and from that you can derive the path name. Each JSP is compiled in to a Servlet, and they each need their own unique class.

For example, in Glassfish, which uses Jasper from Tomcat/Apache:

I have a simple jsp, root.jsp:

<html>
    <body>
        <% out.println(this.getClass()); %>
    </body>
</html>

It is in the root level of the WAR. When I run it, I get this:

class org.apache.jsp.root_jsp

When I created the directory d, and copied the root.jsp in to it, and executed it, I got:

class org.apache.jsp.d.root_jsp

So, you can see, that the class name for /root.jsp is org.apache.jsp.root_jsp. And for /d/root.jsp, it's org.apache.jsp.d.root_jsp.

If you remove org.apache.jsp from the class name, convert any remaining "." to "/", and the "_" to a "." you will get:

/root.jsp
/d/root.jsp

Which effectively gets you what you want.

If you overwrite a JSP at runtime, then it'll start sticking _1 and _2 to the end, so you'll have to account for that.

But play around with this, and it should work for you, and get you where you're going.

Just keep in mind, it's not portable, it clearly works in anything with Jasper (Tomcat, TomEE, Jetty, Glassfish), but I can't say about WebLogic or WebSphere.

But, it might. The container has to map the JSP file to a class somehow, and this is an intuitive way of doing it.

So, try it and see if you can get it to work with your container.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203