JSP from simple to deep (6) — JSP declaration

Now the JSP you write will become a class definition. All the scriptlets you write 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.

In order to add a declaration, you must use <%! And% > circle your statement, for example:

<%@ page import="java.util.*" %>

<%!

Date theDate = new Date();

Date getDate()

{

System. out. println( "In getDate() method" );

return theDate;

}

%>

Hello! The time is Now <%= getDate() %>

The above example has declared variables and methods: a date variable thedate and a method getdate. Both are valid in scriptlets and expressions from now on because they are already defined.

Unfortunately, the above example doesn't work: no matter how you reload the page, the date is the same. The reason is that these statements are calculated only once when the web page 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 to modify the above example and add another function computedate to reinitialize the date. And add a scriptlet to call computedate each time.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>