Feedback Form

JSP Implicit Objects: Full Reference Guide

JSP Implicit Objects: Full Reference Guide

अगर आप JSP (Java Server Pages) सीख रहे हैं, तो “Implicit Objects” एक बहुत ही important और frequently asked topic है — खासकर college exams या interviews में। JSP Implicit Objects वो predefined objects हैं जिन्हें JSP Container अपने आप provide करता है, ताकि developer को बार-बार object बनाने की जरूरत न पड़े। ये objects servlet के environment से जुड़ी सारी useful information रखते हैं — जैसे request, response, session, application आदि।

इस article में हम हर JSP Implicit Object को detail में समझेंगे, उसके purpose, methods और examples के साथ। चलिए step-by-step शुरू करते हैं।

What are JSP Implicit Objects?

JSP Implicit Objects वो built-in objects होते हैं जो JSP Engine द्वारा automatically create किए जाते हैं। इन्हें JSP page में directly use किया जा सकता है बिना किसी declaration या import के। ये total 9 होते हैं और ये servlet classes जैसे HttpServletRequest, HttpServletResponse, HttpSession आदि से internally जुड़े रहते हैं।

List of All 9 JSP Implicit Objects

S.No.Object NameTypeDescription
1requestHttpServletRequestClient द्वारा भेजे गए request data को store करता है।
2responseHttpServletResponseClient को response भेजने के लिए use होता है।
3sessionHttpSessionUser session से related data store करता है।
4applicationServletContextपूरे application के लिए shared data को handle करता है।
5outJspWriterJSP page पर output भेजने के लिए use होता है।
6configServletConfigServlet configuration information रखता है।
7pageContextPageContextJSP page से जुड़ी सारी contextual जानकारी देता है।
8pageObjectCurrent JSP page का reference देता है।
9exceptionThrowableError handling में काम आता है (error pages के लिए)।

1. request Object

request object का use client द्वारा भेजे गए data (जैसे form inputs, parameters, headers) को access करने के लिए किया जाता है। ये object automatically create होता है जब कोई user JSP page को access करता है।

Commonly Used Methods:

  • getParameter(String name) — form से आए parameter को पढ़ने के लिए।
  • getAttribute(String name) — attribute को access करने के लिए।
  • setAttribute(String name, Object value) — attribute set करने के लिए।
  • getHeader(String name) — किसी header का value प्राप्त करने के लिए।

Example:

<%
String user = request.getParameter("username");
out.println("Welcome, " + user);
%>

2. response Object

response object का use server से client को response भेजने के लिए होता है। इसका इस्तेमाल mostly redirect या content type सेट करने में किया जाता है।

Common Methods:

  • sendRedirect(String url) — किसी दूसरे page पर redirect करने के लिए।
  • setContentType(String type) — response content type define करने के लिए।
  • addCookie(Cookie cookie) — cookie जोड़ने के लिए।

Example:

<%
response.setContentType("text/html");
response.sendRedirect("home.jsp");
%>

3. session Object

session object का use user-specific data को maintain करने के लिए किया जाता है, ताकि user जब तक browser बंद न करे तब तक data बना रहे।

Common Methods:

  • setAttribute(String name, Object value) — session में data store करने के लिए।
  • getAttribute(String name) — stored data प्राप्त करने के लिए।
  • invalidate() — session खत्म करने के लिए।

Example:

<%
session.setAttribute("username", "Ravi");
String name = (String) session.getAttribute("username");
out.println("Hello " + name);
%>

4. application Object

application object पूरे web application के लिए common data store करने में use होता है। इसे सभी JSP pages access कर सकते हैं।

Common Methods:

  • setAttribute(String name, Object value)
  • getAttribute(String name)
  • getInitParameter(String name)

Example:

<%
application.setAttribute("siteName", "JavaNotes");
String site = (String) application.getAttribute("siteName");
out.println("Welcome to " + site);
%>

5. out Object

out object JSP page में output भेजने के लिए responsible होता है। ये JspWriter class का object होता है।

Common Methods:

  • print(String s) — simple text print करने के लिए।
  • println(String s) — नई line में text print करने के लिए।
  • flush() — output को browser तक भेजने के लिए।

Example:

<%
out.println("This is JSP Output Example");
%>

6. config Object

config object servlet configuration information देता है, जो deployment descriptor (web.xml) से आती है।

Common Methods:

  • getServletName()
  • getInitParameter(String name)
  • getServletContext()

Example:

<%
String db = config.getInitParameter("database");
out.println("Database: " + db);
%>

7. pageContext Object

pageContext object JSP page की सारी contextual information provide करता है — जैसे attributes, session, request, response इत्यादि को access करना।

Common Methods:

  • getAttribute(String name)
  • setAttribute(String name, Object value)
  • getSession()
  • getRequest()

Example:

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

8. page Object

page object current JSP page का reference होता है। यह basically this keyword की तरह काम करता है।

Example:

<%
out.println("Class of page object: " + page.getClass().getName());
%>

9. exception Object

exception object केवल error pages में use होता है, जहाँ isErrorPage="true" declare किया गया हो। इसका use runtime errors को handle करने के लिए होता है।

Example:

<%@ page isErrorPage="true" %>
<%
out.println("Error: " + exception.getMessage());
%>

Summary Table of JSP Implicit Objects

ObjectScopeMain Use
requestRequestClient request data access करना
responseResponseClient को response भेजना
sessionSessionUser-specific data store करना
applicationApplicationGlobal shared data रखना
outPageOutput भेजना
configApplicationServlet configuration access करना
pageContextPageAll objects access का entry point
pagePageCurrent JSP page reference
exceptionError PageException handling

📒 Exam-Oriented Notes

  • JSP में total 9 implicit objects होते हैं।
  • इनमें से exception केवल error page में use होता है।
  • pageContext एक central object है जो सभी implicit objects को indirectly access करा सकता है।
  • request और session exam में सबसे ज्यादा पूछे जाते हैं।
  • application पूरे web app के लिए global storage देता है।
  • Implicit Objects को JSP container automatically बनाता है — manually declare करने की जरूरत नहीं।
  • इनका internal mapping servlet classes से होता है — जैसे HttpServletRequest, HttpSession आदि।