Category: java
Jsp scriptlets expressions declarations
Published on 30 Sep 2026
Explanation
A JSP scriptlet uses <% %> to embed Java statements directly inside a JSP page.
Code:
<%
String name = "Praveen";
System.out.println(name);
%>
Explanation
A JSP expression uses <%= %> to evaluate a Java expression and write its result to the response.
Code:
<h2>
Welcome <%= "Praveen" %>
</h2>
Explanation
A JSP declaration uses <%! %> to declare fields or methods at the servlet class level.
Code:
<%!
private int count = 0;
public int getCount() {
return count;
}
%>
Explanation
Scriptlets can contain conditions and loops, although modern JSP applications generally prefer JSTL and Expression Language.
Code:
<%
for (int i = 1; i <= 5; i++) {
%>
<p>Number: <%= i %></p>
<%
}
%>
Explanation
Separating Java logic from JSP presentation makes applications cleaner and easier to maintain.
Code:
Preferred:
Servlet β Business Logic
β
request.setAttribute()
β
JSP β HTML + EL + JSTL