Search code examples
javajspmethodsscriptlet

Are methods legal inside JSP scriptlet?


I know its not recommended, and I should be using tag libraries etc etc.

But I'd still like to know if it is legal to declare methods in a JSP scriplet:

<%
   public String doSomething(String param) {
      //
   }

   String test = doSomething("test");

%>

Is that legal? I am getting some weird compile errors (like a ; is expected) that don't seem to fit. Thanks.


Solution

  • You need to use declaration syntax (<%! ... %>):

    <%! 
       public String doSomething(String param) { 
          // 
       } 
    %>
    <%
       String test = doSomething("test"); 
    %>