1

Inside of a JSP page, I was to determine the current path of the page on the file system, for example:

WEB-INF/jsp/currentPage.jsp

I want to be able to jsp:include to another page that is based on the current filesystem path, not the URL the user is using to access the page.

I want to be able to copy/paste the same code to many JSPs and have each page know its filesystem path.

spaetzel
  • 1,252
  • 3
  • 16
  • 23

3 Answers3

0

For referencing other jsp pages (and for other utility methods for JSP), use the JSTL library. Very handy. You can look at this page for reference on how to use it.

Saif Asif
  • 5,516
  • 3
  • 31
  • 48
0

Use geturi method on request:

String uri = request.getRequestURI();
String pageName = uri.substring(uri.lastIndexOf("/")+1);
SMA
  • 36,381
  • 8
  • 49
  • 73
  • That will return the path to the [root of the servlet](http://stackoverflow.com/questions/12160639/what-does-mean-in-the-method-servletcontext-getrealpath), I want the path to the current JSP page. I want to be able to copy/paste the same code to many JSPs and have each page know its filesystem path. – spaetzel Nov 04 '14 at 16:18
  • get uri and parse it to know your jsp page. – SMA Nov 04 '14 at 16:20
  • That doesn't do it either since the JSPs are included into other JSPs. I need the actual filesystem path, not pull it out of the request URI. – spaetzel Nov 04 '14 at 16:31
  • What exactly you need then? Can you add example in your question as well? – SMA Nov 04 '14 at 16:37
0

The answer is available here. Basically, you can use regular expressions on this.getClass() to determine the full path.

In the JSP:

<jsp:include page='<%= SkinController.getSkinPath(this.getClass() ) %>' />

And in SkinController.java

public static String getSkinPath( Class<?> cls ){
    Pattern pattern = Pattern.compile("org.*INF.?.(jsp|tiles).(.*?)_jsp");

    String name = cls.getName();

    Matcher matcher = pattern.matcher(name);

    if( matcher.matches() ){
        String type = matcher.group(1);
        String path = matcher.group(2).replace(".", "/");

        //fullPath is the value we're looking for int he original question
        String fullPath = type.concat("/").concat(path).concat(".jsp");

        return getSkinPath(fullPath);
    }else{
        return "";
    }

}
Community
  • 1
  • 1
spaetzel
  • 1,252
  • 3
  • 16
  • 23