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?

Ninja Asked on 18th September 2018 in Jsp.
Add Comment
1 Answer(s)
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.

Ninja Answered on 18th September 2018.
Add Comment