Is there a way in JSP to know the current page name (not the entire URL or URI)? Of course, we can do something like the following to retrieve the current page name in JSP.
String servletPath=request.getServletPath();
out.println(servletPath.substring(servletPath.lastIndexOf("/")+1, servletPath.length()));
This can retrieve the current page name (may be I'm following the wrong way to do so). Is there a fair and direct way in JSP to retrieve the current page name?
[Also, the separator character /
in this method servletPath.lastIndexOf("/")
should always be independent of any file system supported by any Operating System].
You can do it with URI method also.
String uri = request.getRequestURI();
String pageName = uri.substring(uri.lastIndexOf("/")+1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With