title: When is it appropriate to set a request-scoped variable in a JSP?
body: <p>In my experience, it is rarely/never necessary to set <code>scope="request"</code> on an EL variable.</p>
  
  <p>For example, I have a page that, given an <code>item</code> 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.</p>
  
  <h1>(A) With request-scoped variables</h1>
  
  <h3>itemLink.jsp</h3>
  
  <pre><code>&lt;%-- Accepts a parameter named 'item' --%&gt;
  &lt;c:set var="urlTemplate" value="${param['item'].urlTemplate}" /&gt;
  &lt;c:choose&gt;
    &lt;c:when test="${empty urlTemplate}"&gt;
      &lt;c:set var="itemUrl" scope="request" value="/missingProductUrl.jsp"/&gt;
    &lt;/c:when&gt;
    &lt;c:otherwise&gt;
      &lt;c:url var="itemUrl" scope="request" value="${urlTemplate}"&gt;
        &lt;c:param name="id" value="${param['item'].id}"/&gt;
      &lt;/c:url&gt;
    &lt;/c:otherwise&gt;
  &lt;/c:choose&gt;
  </code></pre>
  
  <h3>otherPage.jsp</h3>
  
  <pre><code>&lt;jsp:include page="itemLink.jsp"&gt;
    &lt;jsp:param name="item" value="${currentItem}"/&gt;
  &lt;/jsp:include&gt;
  
  &lt;%-- 'itemUrl' has request scope --%&gt;
  &lt;a href="${itemUrl}"&gt;Item Link&lt;/a&gt;
  </code></pre>
  
  <h1>(B) Without request-scoped variables</h1>
  
  <h3>itemLink.jsp</h3>
  
  <pre><code>&lt;%-- Accepts a parameter named 'item' --%&gt;
  &lt;c:set var="urlTemplate" value="${param['item'].urlTemplate}" /&gt;
  &lt;c:choose&gt;
    &lt;c:when test="${empty urlTemplate}"&gt;
      &lt;c:set var="itemUrl" value="/missingProductUrl.jsp"/&gt;
    &lt;/c:when&gt;
    &lt;c:otherwise&gt;
      &lt;c:url var="itemUrl" value="${urlTemplate}"&gt;
        &lt;c:param name="id" value="${param['item'].id}"/&gt;
      &lt;/c:url&gt;
    &lt;/c:otherwise&gt;
  &lt;/c:choose&gt;
  
  &lt;c:out value="${itemUrl}"/&gt;
  </code></pre>
  
  <h3>otherPage.jsp</h3>
  
  <pre><code>&lt;c:set var="itemUrl"&gt;
    &lt;jsp:include page="itemLink.jsp"&gt;
      &lt;jsp:param name="item" value="${currentItem}"/&gt;
    &lt;/jsp:include&gt;
  &lt;/c:set&gt;
  
  &lt;%-- 'itemUrl' has page scope --%&gt;
  &lt;a href="${itemUrl}"&gt;Item Link&lt;/a&gt;
  </code></pre>
  
  <p>Is there any reason to use <strong>(A)</strong> over <strong>(B)</strong>? My answer is no, with the following justifications:</p>
  
  <ul>
  <li><p>With <strong>(A)</strong>, you need to remember that any other page processed during the same request will see <code>itemUrl</code>, 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.</p></li>
  <li><p>With <strong>(B)</strong>, none of this is an issue because variables only ever have page scope.</p></li>
  </ul>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Perhaps there's a better solution than <strong>(B)</strong>:</p>
  
  <h1>(C) Using a static include</h1>
  
  <h3>itemLink.jspf</h3>
  
  <pre><code>&lt;%-- Accepts a parameter named 'item' --%&gt;
  &lt;c:set var="urlTemplate" value="${param['item'].urlTemplate}" /&gt;
  &lt;c:choose&gt;
    &lt;c:when test="${empty urlTemplate}"&gt;
      &lt;c:set var="itemUrl" value="/missingProductUrl.jsp"/&gt;
    &lt;/c:when&gt;
    &lt;c:otherwise&gt;
      &lt;c:url var="itemUrl" value="${urlTemplate}"&gt;
        &lt;c:param name="id" value="${param['item'].id}"/&gt;
      &lt;/c:url&gt;
    &lt;/c:otherwise&gt;
  &lt;/c:choose&gt;
  </code></pre>
  
  <h3>otherPage.jsp</h3>
  
  <pre><code>&lt;c:set var="item" value="${currentItem}"/&gt;
  &lt;%@ include page="itemLink.jsp" %&gt;
  
  &lt;%-- 'itemUrl' has page scope --%&gt;
  &lt;a href="${itemUrl}"&gt;Item Link&lt;/a&gt;
  </code></pre>
  
  <p>It's still the case that neither alternative, <strong>(B)</strong> nor <strong>(C)</strong>, requires the use of a request-scoped variable. Is there some reason for using request scope that I've missed?</p>