2 Minutes To Code (β Version)

JSP - 2 minute tutorial

Scriptlet - simple

Scriplets are the real code blocks in jsp. A scriptlet is written by putting plain old java code inside by <% & %> as shown in the examples below.
<html>
	<body>
		<%
			java.util.Date currentDate = new java.util.Date();
		%>
	</body>
</html>


You can even use combinations of jsp sriptlets and expressions as shown in the example below. We will see in the next page, how to combine scriptlets with html code to generate page content on the fly.
Notice that in scriptlets, you write normal java statements, so you need to end all statements with a semicolon(;). However, in jsp expressions, you write java expressions (and not statements), so semicolon(;) must not be used.
<html>
	<body>
		<%
			java.util.Date currentDate = new java.util.Date();
		%>
		
		The current date is <%= currentDate %>
		
	</body>
</html>
The above code will produce this webpage :



As in the case of expressions, there is an alternate way of writing scriptlets that confirms to xml syntax by using tags &
<html>
    <body>
        <jsp:scriptlet>
            java.util.Date currentDate = new java.util.Date();
        >/jsp:scriptlet>
    </body>
</html>