tags:

views:

1152

answers:

4
+7  A: 

You can't use the 'out' variable (nor any of the other "predeclared" scriptlet variables) inside directives.

The JSP page gets translated by your webserver into a Java servlet. Inside tomcats, for instance, everything inside scriptlets (which start "<%"), 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 "<%!") get translated as separate Java methods.

As an example, a very simple page (let's call it foo.jsp):

<html>
    <head/>
    <body>
        <%!
            String someOutput() {
                return "Some output";
            }
        %>
        <% someOutput() %>
    </body>
</html>

would end up looking something like this (with a lot of the detail ignored for clarity):

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("<html>");
        out.write("<head/>");
        out.write("<body>");
        out.write(someOutput()); // i.e. write the results of the method call
        out.write("</body>");
        out.write("</html>");
    }

    // Directive gets translated as separate method - note
    // there is no 'out' variable declared in scope
    private String someOutput()
    {
        return "Some output";
    }
}
Ashley Mercer
Thank you for your nice answer, though it isn't what I'm looking for.
AnSGri
The call to someOutput would not be placed in an out.write statement unless you used the expression syntax <%= %>. When you use the scriptlet syntax it is just inserted inline.
Garth Gilmour
+1  A: 

I suppose this would help:

<%! 
   String someOutput() {
     return "Some Output";
  }
%>
...
<%= someOutput() %>

Anyway, it isn't a good idea to have code in a view.

paradoja
A: 

I'm going to go ahead and say that JSP is a real pain. I strongly recommend you check out something like rails or seaside.

If your only reason for not doing so is avoiding a new language, that's a bad reason. If you're stuck with this tech, I'm sorry.

Marcin
+2  A: 

All you need to do is pass the JspWriter object into your method as a parameter i.e.

void someOutput(JspWriter stream)

Then call it via:

<% someOutput(out) %>

The writer object is a local variable inside _jspService so you need to pass it into your utility method. The same would apply for all the other built in references (e.g. request, response, session).

A great way to see whats going on is to use Tomcat as your server and drill down into the 'work' directory for the '.java' file generated from your 'jsp' page. Alternatively in weblogic you can use the 'weblogic.jspc' page compiler to view the Java that will be generated when the page is requested.

Garth Gilmour