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.
The jsp scriptlet tag can only declare variables not methods. The jsp declaration tag can declare variables as well as methods. The declaration of scriptlet tag is placed inside the _jspService() method.
Declaring Multiple MethodsYou can declare multiple methods in the same JSP declaration, as seen in Listing 3.18.
When the scripting language is set to java, a scriptlet is transformed into a Java programming language statement fragment and is inserted into the service method of the JSP page's servlet. A programming language variable created within a scriptlet is accessible from anywhere within the JSP page.
You can declare any number of variables or methods within one declaration tag, but you have to separate them by semicolons. The declaration must be valid in the scripting language used in the JSP file. You can add method to the declaration part.
You need to use declaration syntax (<%! ... %>
):
<%! public String doSomething(String param) { // } %> <% String test = doSomething("test"); %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With