Now, the JSP file you wrote will be changed to a class definition. All scriptlets you have written should be placed in a method of this class. Similarly, you can add variables and method declarations to this class. Of course, you can also use these variables and methods from scriptlets and expressions.
To add a declaration, you must use <%! And %> to enclose your statement, for example:
<% @ Page import = "Java. util. *" %>
<HTML>
<Body>
<%!
Date thedate = new date ();
Date getdate ()
{
System. Out. println ("in getdate () method ");
Return thedate;
}
%>
Hello! The time is now <% = getdate () %>
</Body>
</Html>
The variables and methods have been declared in the above example: A Date variable thedate and a method getdate. The two are valid in scriptlets and expressions since they have been defined.
However, unfortunately, the above example does not work properly: No matter how heavy the page is, the date is the same. The reason is that these declarations are calculated only once when the webpage is loaded. This is like creating a class in Visual C ++ and defining the initialization value of the variable.
The exercise in this tutorial is: Modify the example above and add another function computedate to reinitialize thedate. Add a scriptlet to call computedate each time.