Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 784
bba Avatar asked Sep 22 '10 12:09

bba


People also ask

Can we write a method inside scriptlet in JSP?

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.

Can we write methods in JSP?

Declaring Multiple MethodsYou can declare multiple methods in the same JSP declaration, as seen in Listing 3.18.

What is true about JSP scriptlet?

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.

How can I declare methods within my 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.


Video Answer


1 Answers

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

<%!     public String doSomething(String param) {        //     }  %> <%    String test = doSomething("test");  %>  
like image 191
axtavt Avatar answered Oct 10 '22 05:10

axtavt