2 Minutes To Code (β Version)

JSP - 2 minute tutorial

JSP Declarations

Jsp declarations are used to declare methods and variables.

You can do this by enclosing the methods/variables in <! & %> as shown below (line no. 4 to 11).

Once declared these can be used through out the jsp page in expressions and scriptlets.

A word of caution is in order here. These declared variables are class level fields and are initialized only once when the jsp is loaded first time by the jsp container(web server). Since the webserver creates a single instance of jsp (actually the underlying servlet), so these variables are shared by all threads using the jsp. Hence are not thread-safe.

So, never put any variables that stores some thread specific state in declaration tag. Put such variables in sriptlets instead.

			java.util.Date initDate = new java.util.Date();
			public java.util.Date getCurrentDate()
				return new java.util.Date();		
		The initDate is <%= initDate %>
		The current date is <%= getCurrentDate() %>
The webpage generated by the above code is :