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.
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- How to config a JSP file in web.xml?
- What is difference between page and pageContext in JSP pages?
- How to disable browser caching for a specific JSP?
- How to retrieve values from HTML form input elements in JSP?
- What is the difference between jsp:forward and response.sendRedirect?
- What is the difference between request.getParameter() and request.getAttribute()?
- How to get the current JSP page name?