Scriptlets and Expressions in JSP

By: Sathya Narayana Viewed: 192 times  Printer Friendly Format    


Scriptlets enclose Java code (on however many lines) that's evaluated within the generated servlet's _jspService() method to generate dynamic content:

<%
// Java code
%>

Take care when using adjacent scriptlet blocks; this code:
<% if(user.isLoggedIn) { %>
<p>Hi!</p>
<% } %>
<% else { %>
<p>Please log in first...</p>
<% } %>

isn't legal because you've broken the else block into two scriptlets.

Expressions
Expressions return a value from the scripting code as a String to the page:
<p>Hello there,
<%= userName %>
Good to see you.</p>




Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial