Feedback Form

pageContext: Attribute Management and Scope Control

Attribute Management and Scope Control in JSP

जब हम Java Server Pages (JSP) के साथ काम करते हैं, तो Attribute Management और Scope Control दो ऐसे concepts हैं जो web application के data flow और lifetime को control करते हैं। ये concepts समझना बहुत जरूरी है क्योंकि यही तय करते हैं कि data कहाँ तक valid रहेगा और कौन उसे access कर सकता है।

What is Attribute in JSP?

JSP में Attribute का मतलब होता है कोई data value जो किसी particular scope में store की जाती है ताकि उसे JSP pages, servlets या beans के बीच share किया जा सके। Attribute basically एक name-value pair के रूप में store होती है।

उदाहरण के लिए — अगर हमें किसी user का नाम multiple JSP pages में use करना है, तो हम उसे एक attribute के रूप में set कर सकते हैं:

request.setAttribute("username", "Ravi");

यहाँ username attribute का नाम है और Ravi उसकी value।

Attributes का उपयोग क्यों करते हैं?

  • Data को JSP pages के बीच share करने के लिए।
  • Session या Application level पर information maintain करने के लिए।
  • Dynamic data को temporary memory में store करने के लिए।

JSP Scopes

JSP में 4 main scopes होते हैं जो ये तय करते हैं कि कोई attribute कब तक और कहाँ तक valid रहेगा। ये scopes JSP के implicit objects से जुड़े होते हैं।

Scope Name Associated Object Lifetime / Visibility
page pageContext केवल current JSP page तक limited होता है।
request request केवल current client request तक valid रहता है।
session session एक user session तक valid रहता है।
application application पूरा application चलने तक valid रहता है।

1. Page Scope

Page scope में रखा गया attribute केवल उसी JSP page के अंदर accessible होता है। जैसे ही page की execution खत्म होती है, attribute भी destroy हो जाता है।

उदाहरण:

pageContext.setAttribute("pageMsg", "Welcome to JSP!"); String msg = (String) pageContext.getAttribute("pageMsg");

यहाँ pageMsg केवल इसी JSP page तक accessible है।

Page Scope कब use करें?

  • जब data केवल temporary या single page तक use करना हो।
  • Example: किसी calculation का short-lived result।

2. Request Scope

Request scope का attribute current HTTP request के दौरान valid रहता है। अगर request को forward या include किया जाता है, तो attribute आगे भी accessible रहता है।

request.setAttribute("userId", 101); RequestDispatcher rd = request.getRequestDispatcher("profile.jsp"); rd.forward(request, response);

अब profile.jsp page में भी userId accessible रहेगा।

Request Scope कब use करें?

  • जब data केवल एक request-response cycle तक use करना हो।
  • Example: Form submission या login validation।

3. Session Scope

Session scope का attribute एक particular user session के दौरान valid रहता है। इसका मतलब है कि जब तक user website पर active है, तब तक उसका data accessible रहेगा।

session.setAttribute("userName", "Ravi Kumar"); String name = (String) session.getAttribute("userName");

यहाँ userName attribute पूरे session में accessible रहेगा।

Session Scope कब use करें?

  • जब data को पूरे user session तक maintain करना हो।
  • Example: User login information या shopping cart data।

4. Application Scope

Application scope का attribute पूरे web application के लिए common होता है। यह तब तक valid रहता है जब तक server या application बंद नहीं होता।

application.setAttribute("totalVisitors", 1500); Integer count = (Integer) application.getAttribute("totalVisitors");

यहाँ totalVisitors पूरे application में accessible रहेगा।

Application Scope कब use करें?

  • जब data सभी users के बीच share करना हो।
  • Example: Website visitor count, configuration settings।

Attribute Management Methods

JSP में attributes को manage करने के लिए चार मुख्य methods का use किया जाता है — setAttribute(), getAttribute(), removeAttribute(), और getAttributeNames()

Method Description
setAttribute(String name, Object value) नए attribute को set करता है या existing को update करता है।
getAttribute(String name) Attribute की value प्राप्त करता है।
removeAttribute(String name) Attribute को current scope से delete करता है।
getAttributeNames() Current scope में सभी attributes के नाम लौटाता है।

Example Code:

session.setAttribute("course", "MCA"); String courseName = (String) session.getAttribute("course"); session.removeAttribute("course");

Scope Priority (Attribute Resolution Order)

अगर एक ही नाम का attribute multiple scopes में मौजूद है, तो JSP उसे नीचे दिए गए priority order में resolve करता है:

  1. page scope
  2. request scope
  3. session scope
  4. application scope

इसका मतलब — अगर एक attribute सभी scopes में मौजूद है, तो JSP पहले page scope वाला use करेगा।

Example:

pageContext.setAttribute("msg", "From Page"); request.setAttribute("msg", "From Request"); session.setAttribute("msg", "From Session"); application.setAttribute("msg", "From Application"); out.print(pageContext.findAttribute("msg"));

Output होगा: From Page, क्योंकि page scope सबसे पहले check किया जाता है।

Using findAttribute() Method

findAttribute() method automatically सभी scopes को ऊपर दिए order में search करता है और पहली matching value को return करता है।

Object obj = pageContext.findAttribute("userName");

अगर attribute किसी भी scope में नहीं मिलता, तो यह null return करता है।

Best Practices for Attribute Management

  • Short-term data के लिए page या request scope use करें।
  • Session tracking के लिए session scope का उपयोग करें।
  • Global configuration data के लिए application scope सबसे अच्छा है।
  • Unnecessary attributes को removeAttribute() से delete करना न भूलें।
  • Data conflict से बचने के लिए unique attribute names रखें।

Difference Between JSP Scopes

Scope Lifetime Accessibility Use Case
page Current JSP execution तक केवल current page Temporary data
request Current HTTP request तक Forwarded pages तक Form data या validation
session User session तक Single user session Login data, user profile
application Server/application lifetime तक All JSP pages Global configuration, analytics

Real-life Example

मान लीजिए एक e-commerce website है —

  • जब user login करता है, तो उसका नाम session scope में store किया जाता है।
  • जब user कोई order submit करता है, तो order details request scope में रखे जाते हैं।
  • किसी temporary calculation के लिए page scope use होता है।
  • Website visitor count application scope में रखा जाता है।

Common Mistakes Students Make

  • हर जगह session scope का use करना — इससे memory leak हो सकता है।
  • Attributes को remove करना भूल जाना।
  • Same attribute name कई scopes में use करना।
  • findAttribute() method का use न करना जिससे data mismatch हो सकता है।

Important Notes for Exams

  • Primary Concept: JSP attributes are stored in one of the four scopes — page, request, session, or application।
  • Key Method: findAttribute() सभी scopes को top-down search करता है।
  • Priority Order: page → request → session → application।
  • Best Practice: Proper scope selection memory optimization के लिए जरूरी है।
  • Exam Tip: Code examples और scope difference table हमेशा याद रखें।