How to get page name in JSP or JSTL?

I want to get the current page name (something like "myPage") using JSP or JSTL. How can i achieve this?

+6
source share
3 answers

You can get it HttpServletRequest#getServletPath() .

 ${pageContext.request.servletPath} 

You can use the JSTL functions taglib to retrieve the extension when necessary.

+16
source

To get the page:

 <% String pageName = com.kireego.utils.Utils.extractPageNameFromURLString(request.getRequestURI()); %> 

and this helper code:

 public static String extractPageNameFromURLString(String urlString){ if (urlString==null) return null; int lastSlash = urlString.lastIndexOf("/"); //if (lastSlash==-1) lastSlash = 0; String pageAndExtensions = urlString.substring(lastSlash+1); int lastQuestion = pageAndExtensions.lastIndexOf("?"); if (lastQuestion==-1) lastQuestion = pageAndExtensions.length(); String result = pageAndExtensions.substring(0,lastQuestion); return result; } 
0
source

Maybe you might think this is javascript, for example:

 var url = window.location.href; 

then use string methods to get the current page name.

-3
source

Source: https://habr.com/ru/post/888145/


All Articles