Modern Alternatives: JSTL , , EL Functions
JSP Scriptlet और इसके Modern Alternatives (JSTL और EL) Explained in Hindi
JSP Scriptlet क्या होता है?
JSP (Java Server Pages) में Scriptlet एक ऐसा code block होता है जहाँ हम सीधे Java code लिख सकते हैं। इसे HTML के अंदर लिखा जाता है और इसका काम dynamic content generate करना होता है। Scriptlet को JSP page में <% ... %> के बीच लिखा जाता है।
Example of JSP Scriptlet
<%
int x = 10;
int y = 20;
int sum = x + y;
out.println("Sum is: " + sum);
%>
ऊपर दिए गए example में Java code सीधे JSP page के अंदर लिखा गया है। जब server इसे process करता है, तो ये code HTML output में convert होकर client को भेजा जाता है।
Scriptlet का Use क्यों किया जाता था?
- Dynamic content generate करने के लिए।
- Server-side logic को directly JSP page में include करने के लिए।
- Database query run करने या data processing के लिए।
Scriptlet अब Deprecated क्यों है?
Scriptlet पहले JSP development में बहुत popular था, लेकिन अब इसे deprecated माना जाता है। Modern JSP applications में Scriptlet का use discouraged किया जाता है। इसके कई कारण हैं:
- Code readability कम हो जाती है क्योंकि HTML और Java एक साथ mix हो जाते हैं।
- Maintenance मुश्किल हो जाती है — logic और presentation एक जगह होने से manage करना कठिन होता है।
- Reusability कम होती है क्योंकि business logic JSP page में फंसी रहती है।
- Security issues भी बढ़ जाते हैं क्योंकि code visibility ज्यादा होती है।
Official Guidelines (Sun/Oracle)
Oracle और Java EE के official documentation के अनुसार, Scriptlets को modern JSP pages में avoid करना चाहिए। इसके बदले developers को JSTL (JSP Standard Tag Library) और Expression Language (EL) use करने की सलाह दी जाती है।
Scriptlet के Modern Alternatives
आज के time में JSP Scriptlets की जगह दो main technologies use की जाती हैं:
- JSTL (JSP Standard Tag Library)
- Expression Language (EL)
1. JSTL (JSP Standard Tag Library)
JSTL एक powerful tag library है जो JSP page से Java logic को अलग करती है। यह HTML जैसा readable syntax provide करती है जिससे page clean और maintainable रहता है।
Common JSTL Tags
| Tag | Use |
|---|---|
| <c:if> | Condition check करने के लिए |
| <c:forEach> | Loop चलाने के लिए |
| <c:choose>, <c:when>, <c:otherwise> | Multiple condition logic के लिए |
| <c:out> | Data safely display करने के लिए |
Example using JSTL
<c:forEach var="i" begin="1" end="5">
<p>Number: ${i}</p>
</c:forEach>
ऊपर के example में कोई भी Java code नहीं लिखा गया, फिर भी output dynamic है। JSTL internally Java logic handle करता है।
2. Expression Language (EL)
EL (Expression Language) JSP page में data access को आसान बनाता है। यह ${} syntax का use करता है जिससे JavaBeans, request parameters, session attributes आदि को access किया जा सके।
Example using EL
<p>User Name: ${user.name}</p>
यहाँ EL expression ${user.name} directly Java object property access कर रहा है, बिना Java code लिखे।
Modern Approach के फायदे
- Clean Separation — Presentation और logic अलग रहते हैं।
- Readability — HTML code simple और maintainable बनता है।
- Less Error-prone — Java syntax errors कम होते हैं।
- Reusability — Business logic अलग files में होने से reuse आसान होता है।
- Standardization — JSTL और EL Java EE standards का हिस्सा हैं।
Scriptlet vs JSTL vs EL
| Feature | Scriptlet | JSTL | Expression Language (EL) |
|---|---|---|---|
| Syntax Type | Java code inside JSP | XML style tags | Short expressions |
| Readability | Low | High | High |
| Maintainability | Difficult | Easy | Easy |
| Recommended | No | Yes | Yes |
| Separation of Logic | Mixed (HTML + Java) | Proper separation | Proper separation |
कब क्या Use करना चाहिए?
- अगर project पुराना है और पहले से Scriptlets में लिखा गया है, तो उसे gradually JSTL और EL में migrate करना चाहिए।
- New projects के लिए हमेशा JSTL और EL prefer करें।
- अगर simple conditional rendering चाहिए, तो
<c:if>tag use करें। - अगर iteration या list rendering चाहिए, तो
<c:forEach>tag perfect है।
JSTL Core Tags Explained
<c:if> Tag
ये tag conditionally HTML elements को render करने के लिए use किया जाता है।
<c:if test="${score >= 50}">
<p>You Passed!</p>
</c:if>
<c:forEach> Tag
ये loop चलाने के लिए use किया जाता है, जैसे array या list को iterate करना।
<c:forEach var="student" items="${studentList}">
<p>${student.name}</p>
</c:forEach>
<c:choose> Tag
यह tag multiple condition handle करने के लिए use किया जाता है।
<c:choose>
<c:when test="${marks >= 75}">Excellent</c:when>
<c:when test="${marks >= 50}">Good</c:when>
<c:otherwise>Fail</c:otherwise>
</c:choose>
Expression Language (EL) Functions
EL में कुछ predefined functions होते हैं जो data manipulation में मदद करते हैं, जैसे length, substring, आदि।
| Function | Use |
|---|---|
| ${fn:length(list)} | List की length बताता है |
| ${fn:contains(str, 'abc')} | String में substring check करता है |
| ${fn:toUpperCase(name)} | Text को uppercase में बदलता है |
Real Life Example: Student Result Display
मान लीजिए आपको student का result show करना है — पहले ये काम Scriptlet से किया जाता था, लेकिन अब JSTL और EL से बहुत आसान हो गया है।
Old Scriptlet Way
<%
String name = "Rahul";
int marks = 80;
if(marks >= 50){
out.println("Pass");
} else {
out.println("Fail");
}
%>
Modern JSTL + EL Way
<c:set var="name" value="Rahul" />
<c:set var="marks" value="80" />
<c:choose>
<c:when test="${marks >= 50}">Pass</c:when>
<c:otherwise>Fail</c:otherwise>
</c:choose>
देखा? Modern approach में readability भी बढ़ी और JSP file भी clean हो गई।
Exam-Oriented Important Points
- Scriptlets JSP के अंदर Java code लिखने का तरीका है (
<% ... %>tags)। - अब Scriptlet outdated है — JSTL और EL इसका replacement हैं।
- <c:forEach> loop चलाने के लिए, <c:if> condition check के लिए use होता है।
- EL में
${}syntax से variable access किया जाता है। - Modern JSP pages में कोई भी Java code नहीं लिखा जाता।
Quick Summary Table
| Concept | Description |
|---|---|
| Scriptlet | JSP में Java code block |
| JSTL | Tag-based library for clean JSP |
| EL | Expression-based data access |
| Replacement | JSTL और EL |
| Recommended | Yes, for modern web apps |