You can let JSTL/EL generate HTML/CSS conditionally based on the URL of the requested JSP page. You can get it by ${pageContext.request.servletPath}
in EL. Assuming that you have the links in some Map<String, String>
in the application scope:
<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're just after a 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 in JSP pages. Put the above in its own menu.jsp
file and include it as follows:
<jsp:include page="/WEB-INF/menu.jsp" />
The page is placed in WEB-INF
folder to prevent direct access.