Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't use the 'out' variable (nor any of the other "predeclared" scriptlet variables) inside directives.</p> <p>The JSP page gets translated by your webserver into a Java servlet. Inside tomcats, for instance, everything inside scriptlets (which start "&lt;%"), along with all the static HTML, gets translated into one giant Java method which writes your page, line by line, to a JspWriter instance called "out". This is why you can use the "out" parameter directly in scriptlets. Directives, on the other hand (which start with "&lt;%!") get translated as separate Java methods.</p> <p>As an example, a very simple page (let's call it foo.jsp):</p> <pre><code>&lt;html&gt; &lt;head/&gt; &lt;body&gt; &lt;%! String someOutput() { return "Some output"; } %&gt; &lt;% someOutput(); %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>would end up looking something like this (with a lot of the detail ignored for clarity):</p> <pre><code>public final class foo_jsp { // This is where the request comes in public void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // JspWriter instance is gotten from a factory // This is why you can use 'out' directly in scriptlets JspWriter out = ...; // Snip out.write("&lt;html&gt;"); out.write("&lt;head/&gt;"); out.write("&lt;body&gt;"); out.write(someOutput()); // i.e. write the results of the method call out.write("&lt;/body&gt;"); out.write("&lt;/html&gt;"); } // Directive gets translated as separate method - note // there is no 'out' variable declared in scope private String someOutput() { return "Some output"; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload