JSP (6)--JSP declaration

Source: Internet
Author: User
Tags date class definition getdate variables variable
JS Now you are writing the JSP to become a class definition. All the scriptlets you write will be placed into one of the methods of this class. Again, you can add variables and methods to declare them in this class. Of course, you can also use these variables and methods from scriptlets and expressions.

In order to add a statement, you must use the <%! and%> to circle your statement, such as:

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

<HTML>

<BODY>

<%!

Date thedate = new Date ();

Date GetDate ()

{

System.out.println ("in GetDate () method");

return thedate;

}

%>

Hello! The time was now <%= getDate ()%>

</BODY>

</HTML>

The above example has declared variables and methods: A Date variable thedate and a method getdate. These two are valid from now on in both scriptlets and expressions, because they are already defined.

Unfortunately, the above example does not work: No matter how you overload the page, the date is the same. The reason for these statements is that they are only counted once when the Web page is loaded. It's like we create a class in Visual C + + and define the initialization value of the variable.

The practice in this tutorial is to modify the example above and add another function computedate to reinitialize the thedate. and 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.