In my experience, it is rarely/never necessary to set scope="request"
on an EL variable.
For example, I have a page that, given an item
parameter, constructs a URL specific to that item based on its properties. This page is included in any page that needs to render a link to an item.
(A) With request-scoped variables
itemLink.jsp
<%-- Accepts a parameter named 'item' --%>
<c:set var="urlTemplate" value="${param['item'].urlTemplate}" />
<c:choose>
<c:when test="${empty urlTemplate}">
<c:set var="itemUrl" scope="request" value="/missingProductUrl.jsp"/>
</c:when>
<c:otherwise>
<c:url var="itemUrl" scope="request" value="${urlTemplate}">
<c:param name="id" value="${param['item'].id}"/>
</c:url>
</c:otherwise>
</c:choose>
otherPage.jsp
<jsp:include page="itemLink.jsp">
<jsp:param name="item" value="${currentItem}"/>
</jsp:include>
<%-- 'itemUrl' has request scope --%>
<a href="${itemUrl}">Item Link</a>
(B) Without request-scoped variables
itemLink.jsp
<%-- Accepts a parameter named 'item' --%>
<c:set var="urlTemplate" value="${param['item'].urlTemplate}" />
<c:choose>
<c:when test="${empty urlTemplate}">
<c:set var="itemUrl" value="/missingProductUrl.jsp"/>
</c:when>
<c:otherwise>
<c:url var="itemUrl" value="${urlTemplate}">
<c:param name="id" value="${param['item'].id}"/>
</c:url>
</c:otherwise>
</c:choose>
<c:out value="${itemUrl}"/>
otherPage.jsp
<c:set var="itemUrl">
<jsp:include page="itemLink.jsp">
<jsp:param name="item" value="${currentItem}"/>
</jsp:include>
</c:set>
<%-- 'itemUrl' has page scope --%>
<a href="${itemUrl}">Item Link</a>
Is there any reason to use (A) over (B)? My answer is no, with the following justifications:
With (A), you need to remember that any other page processed during the same request will see
itemUrl
, and so you should avoid name collisions. It also makes tracing the source of EL variables more difficult, as there's no way to find where a request-scoped variable is set except to search through all pages that are processed during the same request.With (B), none of this is an issue because variables only ever have page scope.
EDIT:
Perhaps there's a better solution than (B):
(C) Using a static include
itemLink.jspf
<%-- Accepts a parameter named 'item' --%>
<c:set var="urlTemplate" value="${param['item'].urlTemplate}" />
<c:choose>
<c:when test="${empty urlTemplate}">
<c:set var="itemUrl" value="/missingProductUrl.jsp"/>
</c:when>
<c:otherwise>
<c:url var="itemUrl" value="${urlTemplate}">
<c:param name="id" value="${param['item'].id}"/>
</c:url>
</c:otherwise>
</c:choose>
otherPage.jsp
<c:set var="item" value="${currentItem}"/>
<%@ include page="itemLink.jsp" %>
<%-- 'itemUrl' has page scope --%>
<a href="${itemUrl}">Item Link</a>
It's still the case that neither alternative, (B) nor (C), requires the use of a request-scoped variable. Is there some reason for using request scope that I've missed?