JSP (6)-JSP Declaration

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.