current page name in JSP

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?

Ans:

You can do it with URI method also.

String uri = request.getRequestURI();

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

資料來源:
https://stackoverflow.com/questions/11130620/how-to-retrieve-the-current-page-name-in-jsp

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *