简体   繁体   English

如何在JSP中检索当前页面名称?

[英]How to retrieve the current page name in JSP?

Is there a way in JSP to know the current page name (not the entire URL or URI)? JSP中是否有一种方法可以知道当前页面名称(而不是整个URL或URI)? Of course, we can do something like the following to retrieve the current page name in JSP. 当然,我们可以执行以下操作来检索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? JSP中是否有一种公平直接的方法来检索当前页面名称?

[Also, the separator character / in this method servletPath.lastIndexOf("/") should always be independent of any file system supported by any Operating System]. [此外,在此方法中,分隔符/ servletPath.lastIndexOf("/")应该始终独立于任何操作系统支持的任何文件系统]。

You can do it with URI method also. 您也可以使用URI方法。

String uri = request.getRequestURI();

String pageName = uri.substring(uri.lastIndexOf("/")+1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM