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:
<%! 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.