In JSP, what is the difference in initializing a variable inside a scriptlet and a declarative?
Answered
In JSP, what is the difference in initializing a variable inside a scriptlet and a declarative?
Best answer
Let us understand the difference with the example below:
1 2 3 4 5 | <%! int x=10; %> <% int y=100; out.println( "Hello...." ); %> |
In this example, ‘x’ is the variable that is declared inside the declarative with a value ’10’ and ‘y’ is the variable that is declared inside a scriptlet with a value ‘100’.
The variable x declared using declaration goes into the servlet class (outside any method) generated from the JSP page which is accessible to all methods of the class whereas the variable y declared inside scriptlet goes into the _jspService() method of the servlet generated, which is accessible only inside the _jspService() method.