JSP from simple to deep (4) — scriptlets
In the above tutorial, we have learned how to embed Java expressions between <% = and% > character series in JSP Web pages. But it's difficult to do a lot of programming by putting Java expressions into HTML.
JSP also allows you to write java code blocks embedded in JSP. The specific method is to place the Java code segment between the <% and% > characters. In fact, it is the same as the expression, but there is no equal sign at the beginning.
Code blocks are known as "scriptlets". Scriptlets alone do nothing for HTML. JSP is called every time the scriptlet contains the executed java code.
The following code is the code after modifying the JSP tutorial in the previous section. We have added a scriptlet:
<%
// This is a scriptlet. Notice that the "date"
// variable we declare here is available in the
// embedded expression later on.
System. out. println( "Evaluating date Now" );
java. util. Date date = new java. util. Date();
%>
Hello! The time is Now <%= date %>
If you run the above example, you will notice that "system. Out. Println" is output in the server log. This is a convenient way to do some simple debugging. Some servers have the function of debugging JSP in the IDE. You can refer to the server documentation to see if it has this function.