What is the diff in declaring a variable inside the declaration tag and scriptlet tag:

When we declare a variable insede the declaration tag it does not go inside the _jspService() method during translation phase.
<%! int x=10;%> - Does not go inside the service method.
But when we declare a variable inside the scriptlet it directly goes inside the _jspService() method during page translation.
<%int x=10;%>- goes inside the service method

Consider this example:
<html>
<body>
<% int count=0;%>
The page count is now:<%=++count%>
</body>
</html>
Every time the output is 1. Because the declaration of count goes inside the _jspService() method, and reset to zero with each request. Like this,
Public void _jspService(HttpServletrequest request,HttpServletResponse response)throws SevletException,IOException{
int count=0;
out.print(++count);}
so here we must declare count as instance variable.
<%!int count=0;%>
Now it does not go into the service method,

Class Myjsp_jsp extends …..{
Int count=0;
Public void _jspService(){}
}

No comments:

Post a Comment