Mahmoud Saleh
Mahmoud Saleh

Reputation: 33625

How to get page name in JSP or JSTL?

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

Upvotes: 9

Views: 22686

Answers (4)

Amgad Hanafy
Amgad Hanafy

Reputation: 667

This line will get you the correct JSP name, it's working also when the page includes multiple pages

<%= this.getClass().getSimpleName().replaceAll("_5F", "").replaceFirst("_", "") %>.jsp<BR> 

Upvotes: 0

Resh32
Resh32

Reputation: 6590

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

Upvotes: 0

piggy_Yu
piggy_Yu

Reputation: 193

maybe you can get it thought javascript way, like:

var url = window.location.href;

then use string methods to get current page name.

Upvotes: -3

BalusC
BalusC

Reputation: 1109715

You can get it by HttpServletRequest#getServletPath().

${pageContext.request.servletPath}

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

Upvotes: 19

Related Questions