Feedback Form

PageContext: Implicit Object Storage, Attribute Scopes, and Error Data

Implicit Object Storage, Attribute Scopes, and Error Data in JSP

Implicit Object Storage क्या होता है?

JSP (Java Server Pages) में Implicit Objects ऐसे predefined objects होते हैं जिन्हें developer को explicitly create करने की जरूरत नहीं होती। ये objects container (जैसे Tomcat server) द्वारा अपने आप बनाए जाते हैं और JSP page पर direct use किए जा सकते हैं।

इन objects का main काम होता है — data store करना, request और response manage करना, session handle करना और user interaction को simplify करना। इन्हें JSP page में सीधे access किया जा सकता है बिना किसी declaration के।

Implicit Objects की List

  • request – Client द्वारा भेजी गई जानकारी रखता है।
  • response – Server से client को भेजा जाने वाला response manage करता है।
  • session – एक user session से जुड़ा data store करता है।
  • application – पूरे web application में data को share करने के लिए।
  • pageContext – JSP page का context manage करता है और अन्य implicit objects तक access देता है।
  • config – JSP के initialization parameters तक पहुंच देता है।
  • out – JSP page पर output भेजने के लिए।
  • page – current JSP page को represent करता है।
  • exception – error handling के लिए।

Attribute Scopes in JSP

अब बात करते हैं Attribute Scopes की — यानी data कहाँ तक accessible रहेगा। JSP में data store और share करने के लिए 4 अलग-अलग scopes होते हैं। हर scope का use-case अलग होता है, और exam में ये बहुत important topic है।

1. page Scope

page scope में रखा गया data सिर्फ current JSP page तक ही valid रहता है। जैसे ही page खत्म होता है, data भी destroy हो जाता है।

Example:

<% pageContext.setAttribute("msg", "Welcome to JSP!"); %> <%= pageContext.getAttribute("msg") %>

2. request Scope

request scope में data एक request-response cycle तक valid रहता है। अगर data को एक JSP से दूसरे JSP या servlet तक भेजना हो, तो request scope perfect होता है।

Example:

<% request.setAttribute("username", "Ravi"); %>

3. session Scope

session scope एक user के पूरे session के दौरान data को store करता है। जब तक user logout नहीं करता या session expire नहीं होता, data उपलब्ध रहता है।

Example:

<% session.setAttribute("user", "Amit"); %> <%= session.getAttribute("user") %>

4. application Scope

application scope में data पूरे web application के लिए common होता है। इसका मतलब है कि सभी users और JSP pages इस data को access कर सकते हैं।

Example:

<% application.setAttribute("collegeName", "Tech University"); %> <%= application.getAttribute("collegeName") %>

Comparison Table of Attribute Scopes

Scope Object Lifetime Accessibility
page pageContext Page के end तक Same JSP page
request request Request complete होने तक Forwarded JSP/Servlet
session session User session तक Same user के सभी JSP pages
application application Server running रहने तक All users and pages

pageContext Implicit Object

pageContext object JSP का सबसे powerful implicit object है। यह बाकी सभी implicit objects (जैसे request, response, session, application) तक access provide करता है। इसके जरिए हम attribute set, get या remove कर सकते हैं।

Important Methods of pageContext

  • setAttribute(String name, Object value) – Attribute store करने के लिए।
  • getAttribute(String name) – Attribute retrieve करने के लिए।
  • removeAttribute(String name) – Attribute delete करने के लिए।
  • findAttribute(String name) – Scope hierarchy में attribute search करने के लिए।
  • getOut() – JSP writer object access करने के लिए।

Example:

<% pageContext.setAttribute("course", "B.Tech", PageContext.SESSION_SCOPE); String course = (String) pageContext.getAttribute("course", PageContext.SESSION_SCOPE); out.println("Course: " + course); %>

ऊपर दिए गए example में हमने pageContext का use करके data को session scope में store और retrieve किया है।

Attribute Hierarchy (findAttribute Behavior)

जब हम findAttribute() method use करते हैं, तो JSP container नीचे दिए गए order में attribute को search करता है:

  • page scope
  • request scope
  • session scope
  • application scope

अगर किसी scope में attribute नहीं मिलता, तो method null return करता है। इस approach से duplicate variable conflicts से बचा जा सकता है।

Error Data in JSP

JSP में error handling बहुत important part है। जब कोई exception या error होती है, तो उसे handle करने के लिए errorPage और isErrorPage attributes का use किया जाता है। इसके साथ ही JSP का exception implicit object भी automatically available होता है error pages पर।

How to define an error page

अगर किसी JSP में error होती है, तो हम उसे किसी dedicated JSP page पर redirect कर सकते हैं जो उस error को handle करे।

Example:

<%@ page errorPage="errorHandler.jsp" %> <% int result = 10/0; %>

Result: <%= result %>

और अब errorHandler.jsp:

<%@ page isErrorPage="true" %>

Error Occurred: <%= exception.getMessage() %>

इस example में जब divide-by-zero error होती है, तो control errorHandler.jsp पर चला जाता है जहाँ exception object उस error का detail दिखाता है।

Important Points about Error Data

  • errorPage और isErrorPage दोनों attributes हमेशा pair में use होते हैं।
  • error page में ही exception implicit object available होता है।
  • हम custom error message show कर सकते हैं ताकि user को system-level error न दिखे।
  • error handling से application का trust और reliability बढ़ती है।

ErrorData Class in JSP

ErrorData class (package: javax.servlet.jsp.tagext) exception के बारे में detailed information रखती है। जब कोई error होती है, तो container इस class के object को बनाकर error page को pass करता है।

Method Description
getThrowable() Actual exception object return करता है।
getStatusCode() HTTP status code return करता है।
getRequestURI() जिस JSP/Servlet में error हुई उसका URI देता है।
getServletName() उस servlet या JSP का नाम देता है जिसमें error हुई।

Practical Example with ErrorData

<%@ page isErrorPage="true" %> <% ErrorData err = pageContext.getErrorData(); out.println("Error Message: " + err.getThrowable().getMessage()); out.println("Status Code: " + err.getStatusCode()); out.println("Request URI: " + err.getRequestURI()); %>

इस code से हमें पता चलता है कि error कहाँ और क्यों हुई — जिससे debugging आसान हो जाती है।

Exam Point of View से Important Notes

  • pageContext implicit object JSP में सबसे ज़्यादा पूछे जाने वाला topic है।
  • चारों attribute scopes के difference को अच्छे से याद करें — table format में लिखने से marks मिलते हैं।
  • exception implicit object सिर्फ error page में use किया जा सकता है।
  • ErrorData class advanced handling के लिए use होती है — इसे extra point के रूप में याद रखें।

Quick Revision Notes

  • Implicit Objects – JSP में predefined objects होते हैं जो data handle करने में मदद करते हैं।
  • Attribute Scopes – page, request, session, application — data lifetime और accessibility define करते हैं।
  • pageContext – बाकी objects का centralized access देता है।
  • Error Handling – errorPage, isErrorPage और exception object के जरिए की जाती है।
  • ErrorData – detailed error information provide करता है।