In this tutorial, we will practice with the JSP expressions, scriptlets and declarations. Expressions are placed between <%= and %>, scriptlets are placed between <% and %> and declarations are placed between <%! and %>.
<h1>A random number is: <%= Math.random() %></h1>
<% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> The time is now: <% // The following scriptlets generate HTML output out.println( String.valueOf( date )); out.println( "<br>Your machine's address is: " ); out.println( request.getRemoteHost()); %>
<%! private int numEntries = 10; private int randomInt(int range) { return(1 + ((int)(Math.random() * range))); } %> <h1>A random list from 1 to 100:</h1> <ul> <% for(int i=0; i<numEntries; i++) { out.println("<li>" + randomInt(100)); } %> </ul>