Are methods legal inside JSP scriptlet?

bba picture bba · Sep 22, 2010 · Viewed 47.2k times · Source

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.

Answer

axtavt picture axtavt · Sep 22, 2010

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

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