JSP Scripting is a mechanism for embedding code fragments directly into an HTML page.There are three types of scriptlets
Scriptlets, Expression and Declaration.
Scriptlets are used to embed any piece of java code into the page.
the syntax for a scriptlet is as follows
<% Scriptlet %>
Scrptlets will get executed at the the time of request.
writing a simple JSP page using Scriptlets
out.println(“This is for scritplet sample”);
Expressions are used to dynamically calculate values to be inserted direcly into the JSp page.These are the elements that are evaluated with the result being converted to Java.lang.String. After the string is converted,it is written to out object. The expression should be enclosed within <%= and %>
while writing expression semicolon cananot be used to end the expression.
Expression format is as follows
<%= Java Expression %>
Sample JSP code using Expression
<title>JSP Expression </title>
<p> <%= 5+5 %><br> // This will display 10
<%= (2*3) + (3+3) %><br> // This will display 12
<%= “Some text ” + (3*3) + (3+3) %><br> // This will display Some text 96
Add a string? <%= “Some text ” + ((3*3) + (3+3)) %><br> // This will display Some text15
JSP declarations are uesd to define methods or variables that are to be used in the java code later in the JSP file. The declaration get inserted into main body of the servlet class. It has the following form .
<%! declaration %>
example : <%! int i=0 %>
1. HTML Commnet
This comment can be viewed fromt he page source from the web browser.
<% — Comment –>
A JSP comment may also contain expression.The following example uses an expression to display the surrent date on the source page
<!– Current date is <%= (new java.util.date()).toLocaleString() %> –>
This will display the the following on the screen
<!– Current date is April 19 ,2011 –>
If you use hidden comment , comments are not sent to the client .The syntax for writing the hidden comment is as follows
<%– Comments –%>