How to get page name in JSP or JSTL?

后端 未结 3 483
我寻月下人不归
我寻月下人不归 2021-01-18 01:31

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

3条回答
  •  深忆病人
    2021-01-18 01:54

    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;
        }
    

提交回复
热议问题