Feedback Form

View: JSP with JSTL/EL – No Java Code

View: JSP with JSTL/EL – No Java Code

Introduction to JSP with JSTL and EL

जब हम Java Web Development करते हैं, तो JSP (Java Server Pages) का use dynamic web pages बनाने के लिए किया जाता है। लेकिन पहले के time में JSP pages में बहुत सारा Java code लिखा जाता था, जिससे readability और maintenance मुश्किल हो जाती थी। इसी problem को solve करने के लिए JSTL (JavaServer Pages Standard Tag Library) और EL (Expression Language) का use किया गया। इन दोनों के use से हम JSP को clean, readable और easy to maintain बना सकते हैं — और सबसे बड़ी बात, अब JSP में कोई Java code नहीं लिखना पड़ता।

Why No Java Code in JSP?

पहले के JSP pages में scriptlets (<% %>) और declarations लिखे जाते थे, जैसे Java code directly JSP में use होता था। लेकिन ये approach MVC pattern को break कर देता था, क्योंकि logic और view दोनों mix हो जाते थे। अब modern development में JSTL और EL का use किया जाता है ताकि View part सिर्फ presentation संभाले और business logic Servlet या DAO में रहे।

Problems with Java Code in JSP

  • Code readability बहुत खराब हो जाती है।
  • Maintenance और debugging में दिक्कत आती है।
  • Designers और developers के बीच coordination मुश्किल होता है।
  • MVC (Model View Controller) principle का violation होता है।

What is JSTL?

JSTL का full form है JavaServer Pages Standard Tag Library। यह JSP के लिए predefined tags का collection है जो basic functionalities जैसे iteration, condition checking, database access और formatting provide करता है।

JSTL Libraries

JSTL को चार main libraries में divide किया गया है:

Library Prefix Purpose
Core Tags c Conditional statements, looping, variable setting आदि के लिए।
Formatting Tags fmt Date, number और message formatting के लिए।
SQL Tags sql Database interaction के लिए।
XML Tags x XML data processing के लिए।

Core JSTL Example

यहाँ एक simple example है जो दिखाता है कि हम JSTL से loop और condition कैसे handle कर सकते हैं:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<ul>
<c:forEach var="item" items="${productList}">
<li>${item.name} - ${item.price}</li>
</c:forEach>
</ul>

ऊपर दिए गए example में कोई Java code नहीं लिखा गया है। सारा logic JSTL tags और EL (Expression Language) के जरिए manage हुआ है।

What is Expression Language (EL)?

EL का use JSP में data को access करने के लिए किया जाता है। यह बहुत simple और readable syntax provide करता है। EL के जरिए हम request, session, application scope में stored data को easily access कर सकते हैं।

EL Syntax

EL का basic syntax होता है ${expression}। उदाहरण के लिए:

${student.name}
${sessionScope.userEmail}
${param.id}

ऊपर दिए गए expressions JSP में बिना किसी Java code के data access करते हैं। यह readability बढ़ाता है और JSP को HTML जैसा clean बनाता है।

Advantages of Using JSTL and EL

  • JSP pages में readability और simplicity बढ़ती है।
  • Java code completely remove हो जाता है।
  • MVC pattern maintain रहता है।
  • Developers और designers का काम अलग-अलग हो सकता है।
  • Code reusable और maintainable बनता है।
  • Tag-based syntax से error chances कम होते हैं।

Difference Between JSP With and Without JSTL/EL

Feature Traditional JSP (with Java code) Modern JSP (with JSTL/EL)
Code Type Java + HTML mixed Pure HTML + JSTL Tags
Readability Complex and confusing Clean and simple
MVC Compatibility Low High
Maintenance Difficult Easy
Debugging Time consuming Quick and efficient

How to Use JSTL and EL in JSP

Step 1: Add JSTL Library

JSTL का use करने के लिए पहले आपको JSTL jar files को project में add करना होता है। अगर आप Maven use कर रहे हैं तो dependency कुछ इस तरह होगी:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

Step 2: Import JSTL Tag Library in JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Step 3: Use EL for Data Access

<p>Welcome, ${user.name}!</p>

अब आप अपने JSP page में EL के जरिए dynamic data display कर सकते हैं, बिना किसी Java scriptlet के।

Common JSTL Core Tags

Tag Description
<c:out> Data को output करने के लिए।
<c:if> Condition check करने के लिए।
<c:choose> Multiple conditions handle करने के लिए।
<c:forEach> Loop चलाने के लिए।
<c:set> Variable assign करने के लिए।

Example of <c:if> and <c:choose>

<c:if test="${user.role == 'admin'}">
Welcome Admin!
</c:if>

<c:choose>
<c:when test="${marks >= 60}">
Pass
</c:when>
<c:otherwise>
Fail
</c:otherwise>
</c:choose>

Best Practices for JSP with JSTL and EL

  • JSP में कभी भी scriptlet (<% %>) का use न करें।
  • Business logic हमेशा Servlet या Controller में रखें।
  • View में सिर्फ presentation logic होना चाहिए।
  • EL के जरिए data access करें, JSTL से control flow manage करें।
  • JSP files को .jsp extension में रखें और views folder में store करें।
  • Common layouts के लिए <jsp:include> या tag files use करें।

Real-Life Example: Displaying Student List

मान लीजिए हमारे पास एक Servlet है जो student list को request में set करता है। अब हम JSP में JSTL और EL का use करके उसे display करेंगे:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<h2>Student List</h2>
<table border="1">
<tr><th>ID</th><th>Name</th><th>Course</th></tr>
<c:forEach var="student" items="${studentList}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.course}</td>
</tr>
</c:forEach>
</table>

यह example clearly दिखाता है कि बिना एक भी Java code लिखे हम पूरा dynamic table बना सकते हैं।

Exam Notes (Important Points)

  • JSTL का full form – JavaServer Pages Standard Tag Library
  • EL का syntax – ${expression}
  • JSTL core library prefix – c
  • JSTL और EL का use JSP को Java code से free करता है।
  • JSTL tags predefined होते हैं और XML जैसे दिखते हैं।
  • EL का use request, session, application data access करने के लिए होता है।
  • MVC model में JSP सिर्फ View layer के लिए use होती है।
  • Scriptlet (<% %>) का use modern JSP में नहीं करना चाहिए।
  • Readability और maintenance JSTL/EL से काफी improve होती है।