Hackforge Academy

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

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community