JSP simple exercise-a simple counter
In JSP, the program code written between "<%" and "%>" becomes a java program piece.
A JSP page can contain multiple java program slices. Note that the variables declared in the Java program are valid in the program and expressions used on the JSP page where they are located. Based on this, you can divide a large program into several small ones, and insert HTML statements into the program to make the program code more readable.
Variables declared in the program are only valid on the page and are local variables. They cannot be shared when different customers access the same page. However, if it is in "<%! Variables declared between "and" %> "can be shared among different customers. The valid range is the entire Web application. After the server is disabled, the variables will be released.
"<% =" And "%>" can be used to directly output the value of a variable or expression. The value of a variable or expression will be output as a string in the browser. This method is commonly used in JSP programming, especially when mixed with HTML tags. The following code:
<% @ Page contentType = "text/html; charset = gb2312" %> <%! Int counter = 0; synchronized void counterFunction () {counter ++ ;}%> <% counterFunction () ;%> website counter
You are the <% = counter %> visitor
This program first runs in "<%! A counter variable and counting method are declared between "and" %> ". The counter variable will be shared among customers until the server is closed. The counting method is actually adding 1 to the counting variable. The sychronized keyword is added before the method. This keyword can prevent conflict when the customer calls this method to change the value of the counting variable at the same time, therefore, the method is serialized.