Feedback Form

Implicit Objects in EL: $

Implicit Objects in EL (Expression Language) in JSP

Introduction to EL (Expression Language)

जब हम JSP (Java Server Pages) में काम करते हैं, तो अक्सर हमें Java code और HTML code को एक साथ mix करना पड़ता है। पहले ये काम Scriptlets से होता था, लेकिन अब JSP Expression Language यानी EL का use करके हम data को बहुत आसान तरीके से access कर सकते हैं।

EL का main purpose है — JSP pages में dynamic content को simple और readable तरीके से access करना, बिना ज़्यादा Java code लिखे। और इसी EL में कुछ built-in Implicit Objects दिए गए हैं जो data handling को बहुत easy बना देते हैं।

What are Implicit Objects in EL?

Implicit Objects वो predefined objects होते हैं जो JSP में automatically available रहते हैं। इन्हें use करने के लिए हमें अलग से define करने की जरूरत नहीं होती। Expression Language में ये objects application, request, session, param आदि data को directly access करने में मदद करते हैं।

Example के तौर पर, अगर आप request parameter का data लेना चाहते हैं, तो EL में simple syntax होता है:

${param.username}

इसका मतलब है कि request के अंदर जो “username” parameter आया है, उसका value show होगा।

List of Implicit Objects in EL

EL में लगभग 11 Implicit Objects predefined होते हैं। हर object का अपना purpose और scope होता है। चलिए इन्हें एक-एक करके detail में समझते हैं।

Implicit Object Scope / Use Description
${pageScope} Page Current JSP page में defined attributes को access करता है।
${requestScope} Request Current HTTP request के scope में available attributes को access करता है।
${sessionScope} Session Current user session में stored attributes को access करने के लिए।
${applicationScope} Application Entire web application में stored attributes को access करता है।
${param} Request Request parameters (form data) के values को access करने के लिए।
${paramValues} Request अगर किसी parameter के multiple values हैं, तो उन्हें array form में access करता है।
${header} Request Request header के values को access करता है।
${headerValues} Request Multiple header values को array के रूप में return करता है।
${cookie} Request Client-side cookies को access करने के लिए।
${initParam} Application web.xml में defined initialization parameters को access करता है।
${pageContext} Page Current page context (including session, request, response) को access करने के लिए।

Detailed Explanation of Each Implicit Object

1. pageScope

pageScope object का use सिर्फ current JSP page के अंदर define किए गए variables या attributes को access करने के लिए होता है। इसका scope सिर्फ उस page तक सीमित रहता है।

${pageScope.variableName}

अगर कोई variable same name से multiple scopes में है, तो pageScope सबसे पहले check होता है।

2. requestScope

requestScope object current HTTP request के अंदर stored attributes को access करता है। जब तक request complete नहीं हो जाती, तब तक ये data valid रहता है।

${requestScope.userData}

इसका use तब होता है जब हम data को servlet से JSP में forward करते हैं।

3. sessionScope

sessionScope object का use user-specific data को store या access करने के लिए किया जाता है जो पूरे session के दौरान available रहता है। ये तब तक रहता है जब तक user browser बंद न करे या session expire न हो जाए।

${sessionScope.userEmail}

Example: अगर user login करता है तो उसका username sessionScope में store किया जा सकता है ताकि वो हर page पर available रहे।

4. applicationScope

applicationScope पूरे web application के लिए common data को store करने के काम आता है। इस scope का lifetime पूरे application के बराबर होता है।

${applicationScope.totalVisitors}

Example: Website पर कितने users visit कर चुके हैं, ये count applicationScope में रखा जा सकता है।

5. param

param object request parameters को access करने के लिए सबसे simple और direct तरीका है। जैसे कि HTML form से आने वाले data को लेना।

${param.email}

Example: अगर form से “email” नाम का field submit हुआ है, तो ${param.email} उसका value दिखाएगा।

6. paramValues

paramValues object तब काम आता है जब किसी HTML form में same name के multiple input fields हों। ये उन्हें array के रूप में return करता है।

${paramValues.languages[0]}

Example: अगर user ने multiple checkboxes select किए हैं (languages जैसे — Java, Python, C++), तो उन्हें access करने के लिए paramValues का use होगा।

7. header

header object HTTP request के header values को access करने में मदद करता है। जैसे कि browser name, content type, etc.

${header["user-agent"]}

यहाँ “user-agent” client browser की detail बताएगा।

8. headerValues

headerValues object का use तब होता है जब किसी header के multiple values मौजूद हों। ये उन सभी को array के रूप में return करता है।

${headerValues["accept-language"][0]}

9. cookie

cookie object client-side cookies को access करने के लिए use होता है। इससे हम cookies के value को read कर सकते हैं।

${cookie.userName.value}

यहाँ “userName” cookie का actual value display होगा।

10. initParam

initParam object web.xml में defined initialization parameters को access करता है। ये पूरे application के लिए common होते हैं।

${initParam.dbUsername}

Example: अगर web.xml में database username defined है, तो उसे सीधे EL से access किया जा सकता है।

11. pageContext

pageContext object सबसे powerful EL object है, जो current JSP page के almost हर element को represent करता है — request, response, session, out, exception, आदि।

${pageContext.request.method}

यहाँ “request.method” HTTP method (GET या POST) को show करेगा।

Practical Example of EL Implicit Objects

मान लीजिए हमारे पास एक simple JSP form है जहाँ user अपना नाम और email submit करता है, और हम उन्हें next JSP page में display करना चाहते हैं।

Example Code:

<form action="display.jsp" method="post">
Name: <input type="text" name="username"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>

अब "display.jsp" page में हम EL का use करके values को directly access कर सकते हैं:

<p>Name: ${param.username}</p>
<p>Email: ${param.email}</p>

इस तरह EL हमें बिना scriptlet के clean और readable code लिखने की सुविधा देता है।

Difference Between EL and Scriptlet

Feature Scriptlet Expression Language (EL)
Syntax Complex (Java code required) Simple and clean
Readability Low High
Code Maintenance Difficult Easy
Scope Access Manual Automatic via implicit objects

Benefits of Using EL Implicit Objects

  • Code clean और readable बनता है।
  • Scriptlet code की जरूरत नहीं पड़ती।
  • Different scopes (page, request, session, application) से data आसानी से access होता है।
  • Maintenance और debugging आसान होती है।
  • Exam point of view से बहुत important topic है।

Important Notes for Exam

  • EL का full form है — Expression Language।
  • EL में total 11 implicit objects available होते हैं।
  • Implicit Objects predefined होते हैं — इन्हें define करने की जरूरत नहीं होती।
  • ${param} का use form data access करने के लिए किया जाता है।
  • ${sessionScope} user session के data को access करने के लिए।
  • ${applicationScope} पूरे web application के common data के लिए।
  • ${cookie} client cookies को access करने के लिए।
  • ${pageContext} सबसे powerful object है — यह हर scope तक पहुंच देता है।
  • EL का use readability और performance दोनों improve करता है।
  • College exams में इस topic से short और long both type questions आते हैं।

Short Summary

EL Implicit Objects JSP development को आसान बनाते हैं क्योंकि इनके जरिए हम directly JSP scopes, request parameters, cookies, headers और context data access कर सकते हैं।

अगर आप JSP पढ़ रहे हैं या exam की तैयारी कर रहे हैं, तो EL के ये objects बहुत helpful रहेंगे क्योंकि ये practically हर JSP application में use होते हैं।