title: jsp useBean scope in included page components
body: <p>I'm creating a site on which there are many page components that refer to objects that are used elsewhere in the page.  Some of these components are used in other pages as well, thus my desire to use includes rather than cut-and-paste.  </p>
  
  <p>The problem is that if I have identical "jsp:useBean" statements in multiple components of the page, the compiler gives the error "duplicate local variable".  </p>
  
  <p>How can I use the same objects in multiple components of a JSP page without having everything in the file?</p>
  
  <p>Example:  in the main (parent) page:</p>
  
  <pre><code>&lt;jsp:useBean id="fred" class="..." scope="request" /&gt;
  
  &lt;jsp:directive.include file="myWidget.jsp" /&gt;
  </code></pre>
  
  <p>the included page, myWidget.jsp, also needs to make use of the "fred" object:</p>
  
  <pre><code>&lt;jsp:useBean id="fred" class="..." scope="request" /&gt;
  </code></pre>
  
  <p>However, this causes compilation to fail with "duplicate local variable 'fred'".</p>
  
  <p>This basically defeats the idea of having a page broken up into components; I don't want to have JSP files that are hundreds of lines long, that are only slightly different from one another!</p>
  
  <p>For one component, where it's absolutely essential that it be consistent across every page of the site, I've worked around the problem with a scriptlet:</p>
  
  <pre><code>&lt;%
      { // for scope
          Object _o1 = request.getAttribute("fred");
          if (_o1 !=null &amp;&amp; _o1 instanceof MyClass)
          {
              // use a deliberately obscure variable name to avoid conflict
              MyClass _myLocalFred1 = (MyClass) _o1; 
  
          }
      } // for scope
  %&gt;
  </code></pre>
  
  <p>This works, but it's ugly; several books say "don't ever use scriptlets", but scriptlets seem to be the only method where variable scope can be controlled, and I can use names without worrying that they're in use elsewhere in the page.</p>
  
  <p>How does one use jsp:useBean in an include'd file?</p>