Programming Tutorials

Scriptlets and Expressions in JSP

By: Sathya Narayana in JSP Tutorials on 2010-10-24  

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>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JSP )

Latest Articles (in JSP)