title: How to create a layout template using JSP?
body: <p>I am attempting to create a layout template to re-use in various JSP pages so certain common HTML (scripts, styles, meta tags, etc) don't have to be duplicated everywhere. </p>
  
  <p>I am not very familiar with JSP but from what I can tell the way to do this is using tag files. What I have almost works but it appears that JSPs do not accept JSP/Java code within the body of a tag?</p>
  
  <p>My template tag in <code>WEB-INF/tags/layouts/base.tag</code>:</p>
  
  <pre><code>&lt;%@tag description="Base page layout" pageEncoding="UTF-8" %&gt;
  &lt;%@attribute name="title" fragment="true" %&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;&lt;jsp:invoke fragment="title" /&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;jsp:doBody /&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is <code>some-page.jsp</code>:</p>
  
  <pre><code>&lt;%@taglib prefix="layout" tagdir="/WEB-INF/tags/layouts" %&gt;
  
  &lt;% String txt = "Some text..."; %&gt;
  
  &lt;layout:base&gt;
    &lt;jsp:attribute name="title"&gt;Hello!&lt;/jsp:attribute&gt;
    &lt;jsp:body&gt;
      &lt;h1&gt;&lt;%= txt %&gt;&lt;/h1&gt; 
    &lt;/jsp:body&gt;
  &lt;/layout:base&gt;
  </code></pre>
  
  <p>The <code>&lt;h1&gt;</code> tag renders but the content from the variable does not. I have also tried using <code>&lt;c:set var="txt" value="${txt}" /&gt;</code> and many other variations but nothing seems to work.</p>
  
  <p>Is there a way to make this work or another approach that I'm not seeing? I cannot find anything on the web that explains how to accomplish this.</p>
  
  <p>This seems like a very simple and common thing to want to do with templates and from every templating language I've used in the JavaScript world, this is supported but I'm surprised this isn't easier in JSP.</p>