How to get page name in JSP or JSTL?

2019-02-22 02:59发布

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

标签: jsp jstl el
3条回答
\"骚年 ilove
2楼-- · 2019-02-22 03:18

You can get it by HttpServletRequest#getServletPath().

${pageContext.request.servletPath}

You can use the JSTL functions taglib to extract the extension whenever necessary.

查看更多
一夜七次
3楼-- · 2019-02-22 03:24

maybe you can get it thought javascript way, like:

var url = window.location.href;

then use string methods to get current page name.

查看更多
时光不老,我们不散
4楼-- · 2019-02-22 03:37

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;
    }
查看更多
登录 后发表回答