Google News
logo
JSP - Interview Questions
What is Scriptlet, Expression and Declaration in JSP?
Scriptlets, Expression and Declaration are scripting elements in JSP page using which we can add java code in the JSP pages.
 
A scriptlet tag starts with <% and ends with %>. Any code written inside the scriptlet tags go into the _jspService() method. For example;
<%
Date d = new Date();
System.out.println("Current Date="+d);
%>
Since most of the times we print dynamic data in JSP page using out.print() method, there is a shortcut to do this through JSP Expressions. JSP Expression starts with <%= and ends with %>.
 
<% out.print("Learning"); %> can be written using JSP Expression as <%= "Learning" %>
 
Notice that anything between <%= %> is sent as parameter to out.print() method. Also notice that scriptlets can contain multiple java statements and always ends with semicolon (;) but expression doesn’t end with semicolon.
 
JSP Declarations are used to declare member methods and variables of servlet class. JSP Declarations starts with <%! and ends with %>.
 
For example we can create an int variable in JSP at class level as <%! public static int count=0; %>.
Advertisement