FAQ


Advertisement



Can I declare static variables by using the JSP declaration?

Yes, You can. For example,

<%! static public int counter = 0; %>

The effect of this declaration is to create an integer variable named counter that is shared by all instances of the page's servlet class. If any one instance changes the value of this variable , all instances see the new value. In practice, because the JSP container typically creates only one instance of the servlet class representing a particular JSP page, there is a little difference between declaring instance variables and class variables. The major exception to this rule is when a JSP page sets the isThreadSafe attribute of the page directive to false, indicating that the page is not thread-safe. In this case, the JSP container may create multiple instances of the page's servlet class, in order to handle multiple simultaneous requests,one request per instance. Now to share a variable 's value across multiple requests under these circumstances, the variable must be declared as static: class variable , rather than an instance variable.

When the isThreadSafe attribute is true, which is default, however , it makes little practical difference. Declaring as instance variable saves a little bit typing.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |