1

How I can match the current page URL with a certain pattern. For example I want to make if statement that makes one region on the page appears or disappears depends on the URL pattern of the current JSP page.

What I know , I have to use the tag :

<c:if test="the conditional test">Region</c:if>

for example I want the region appears if the end of URL is matching /me/* ?

Hatem
  • 109
  • 1
  • 3
  • 10
  • How are you reaching this page? Is this the first page that you want to show up? – aksappy Feb 18 '13 at 05:19
  • @aksappy as i think it is not important how to reach the page. Anyway am reaching the page using controllers technique. – Hatem Feb 18 '13 at 05:31
  • this link may be fulfill your requirement [here][1] [1]: http://stackoverflow.com/questions/5096023/display-forwarded-jsp-with-url-pattern – Himanshu Feb 18 '13 at 05:46
  • @Himanshu Thanks. But it is not what I mean – Hatem Feb 18 '13 at 06:02

2 Answers2

1

You can use

HttpServletRequest#getRequestURI()

to obtain the request URI. The

getServletPath()

as suggested by the other answer is not necessarily helpful as it represents the servlet path (the matching part in the JSP/Servlet URL pattern), not the request URI (as the enduser sees in the browser address bar). If the JSP was been forwarded by some front controller servlet, you would get the JSP's own path instead of the virtual path as in the browser address bar.

Assuming that you have a menu which is represented by a List in the application scope where the Page class has url and name properties

you can use this code to find current page url, and then do your task

<c:set var="active" value="${fn:endsWith(pageContext.request.requestURI, page.url)}" />
Raj Adroit
  • 3,828
  • 5
  • 31
  • 44
1

Something along the lines of:

<c:set var="url" value="${pageContext.request.requestURL}" />

<c:set var="pathinfo" value="${fn:split(url, '/')}" />
<c:set var="pathnode" value="${pathinfo[pathinfo.length - 1]}" />

<c:if test="${pathnode == 'me'}">
   <p>Show this region</p>
</c:if>
tbsalling
  • 4,477
  • 4
  • 30
  • 51