You can let JSTL / EL generate HTML / CSS conditionally based on the URL of the requested JSP page. You can get it ${pageContext.request.servletPath} in EL. Assuming you have links in some Map<String, String> in the application area:
<ul id="menu"> <c:forEach items="${menu}" var="item"> <li> <c:choose> <c:when test="${pageContext.request.servletPath == item.value}"> <b>${item.key}</b> </c:when> <c:otherwise> <a href="${item.value}">${item.key}</a> </c:otherwise> </c:choose> </li> </c:forEach> </ul>
Or when you are only after the CSS class
<ul id="menu"> <c:forEach items="${menu}" var="item"> <li><a href="${item.value}" class="${pageContext.request.servletPath == item.value ? 'active' : 'none'}">${item.key}</a></li> </c:forEach> </ul>
You can use <jsp:include> to reuse content on JSP pages. Put the above into your own menu.jsp file and include it as follows:
<jsp:include page="/WEB-INF/menu.jsp" />
The page is placed in the WEB-INF folder to prevent direct access.
source share