Feedback Form

Introduction to 9 Implicit Objects: request, response, out, session, application

Introduction to 9 Implicit Objects in JSP: request, response, out, session, application

Introduction to JSP Implicit Objects

जब हम JSP (Java Server Pages) पर काम करते हैं, तो हमें कुछ ऐसे objects अपने आप मिल जाते हैं जिन्हें manually create करने की जरूरत नहीं होती। इन्हें ही Implicit Objects कहा जाता है। JSP container अपने आप इन्हें बनाता है ताकि हम web application के अंदर data को आसानी से manage कर सकें। इनका main purpose है कि developer को request, response, session जैसी चीज़ों को handle करने के लिए बार-बार code ना लिखना पड़े।

JSP में कुल 9 Implicit Objects होते हैं। ये हैं: request, response, out, session, application, config, page, pageContext, और exception। हर object का अपना role और scope होता है, और ये सभी server side पर काम करते हैं।

What are Implicit Objects?

Implicit Objects ऐसे pre-defined objects हैं जो JSP container के द्वारा automatically provide किए जाते हैं। इनका use data transfer, user interaction, और web application control के लिए किया जाता है। इनका सबसे बड़ा फायदा यह है कि आपको Servlet की तरह boilerplate code नहीं लिखना पड़ता।

List of 9 JSP Implicit Objects

Object Name Class Scope Usage
request javax.servlet.http.HttpServletRequest Request Scope Client से आए data को प्राप्त करने के लिए
response javax.servlet.http.HttpServletResponse Response Scope Client को output भेजने के लिए
out javax.servlet.jsp.JspWriter Page Scope Output stream में data लिखने के लिए
session javax.servlet.http.HttpSession Session Scope User-specific data को store करने के लिए
application javax.servlet.ServletContext Application Scope पूरे web app के लिए common data रखने के लिए
config javax.servlet.ServletConfig Application Scope Servlet configuration access करने के लिए
pageContext javax.servlet.jsp.PageContext Page Scope सभी implicit objects को manage करने के लिए
page java.lang.Object Page Scope Current JSP page को represent करता है
exception java.lang.Throwable Page Scope (error page) Error handling के लिए

1. request Object

request object का use client से आने वाले data (जैसे form input, parameters, cookies, headers आदि) को receive करने के लिए किया जाता है। ये object हर बार तब बनता है जब कोई client request भेजता है।

Important Methods of request Object

  • getParameter(String name) – किसी input field का value लेने के लिए।
  • getAttribute(String name) – Attribute को access करने के लिए।
  • setAttribute(String name, Object value) – Attribute set करने के लिए।
  • getRequestDispatcher(String path) – किसी JSP या Servlet को forward/include करने के लिए।

Example:

<% String name = request.getParameter("username"); %>
<p>Welcome, <%= name %></p>

2. response Object

response object का use client को data भेजने के लिए किया जाता है। ये object HttpServletResponse class का होता है और HTTP response control करने के लिए इस्तेमाल होता है।

Important Methods of response Object

  • sendRedirect(String url) – किसी दूसरी page पर redirect करने के लिए।
  • setContentType(String type) – Response का type define करने के लिए।
  • addCookie(Cookie cookie) – Cookie add करने के लिए।
  • setHeader(String name, String value) – HTTP header set करने के लिए।

Example:

<% response.sendRedirect("home.jsp"); %>

3. out Object

out object का use output stream में data लिखने के लिए किया जाता है। यह JspWriter class का instance होता है और HTML content को client browser तक भेजता है।

Important Methods of out Object

  • print() – Simple output print करने के लिए।
  • println() – Output के बाद new line के साथ print करने के लिए।
  • clear() – Output buffer को clear करने के लिए।
  • flush() – Output stream को flush करने के लिए।

Example:

<% out.println("Hello JSP!"); %>

4. session Object

session object user-specific data को पूरे session के दौरान store करने के लिए इस्तेमाल किया जाता है। यह HttpSession class का object होता है और user के logout या timeout होने तक active रहता है।

Important Methods of session Object

  • setAttribute(String name, Object value) – Session में data set करने के लिए।
  • getAttribute(String name) – Data को retrieve करने के लिए।
  • invalidate() – Session को destroy करने के लिए।
  • getId() – Session ID प्राप्त करने के लिए।

Example:

<% session.setAttribute("user", "Amit"); %>
<% String user = (String) session.getAttribute("user"); %>
<p>Welcome <%= user %></p>

5. application Object

application object पूरे web application के लिए common data को store करने के काम आता है। यह ServletContext class का object होता है और server start से लेकर stop तक active रहता है।

Important Methods of application Object

  • setAttribute(String name, Object value) – Application level data set करने के लिए।
  • getAttribute(String name) – Data को retrieve करने के लिए।
  • getInitParameter(String name) – web.xml में दिए गए parameters प्राप्त करने के लिए।

Example:

<% application.setAttribute("counter", 1); %>
<% int count = (int) application.getAttribute("counter"); %>
<p>Visitors: <%= count %></p>

6. config Object

config object का use Servlet configuration parameters को access करने के लिए किया जाता है। यह ServletConfig class का instance होता है और initialization values प्रदान करता है।

Example:

<% String driver = config.getInitParameter("jdbcDriver"); %>
<p>Database Driver: <%= driver %></p>

7. pageContext Object

pageContext object JSP page की सभी information और implicit objects को manage करने के लिए इस्तेमाल होता है। यह एक central point होता है जहाँ से आप page, request, session, और application को access कर सकते हैं।

Important Methods:

  • getSession() – Current session object देता है।
  • getRequest() – Current request object देता है।
  • setAttribute(String name, Object value) – Data set करने के लिए।
  • getAttribute(String name) – Data retrieve करने के लिए।

Example:

<% pageContext.setAttribute("city", "Delhi"); %>
<p>City: <%= pageContext.getAttribute("city") %></p>

8. page Object

page object current JSP page को represent करता है। यह java.lang.Object class का instance होता है और “this” keyword की तरह behave करता है।

Example:

<% out.println(page.getClass().getName()); %>

9. exception Object

exception object केवल error pages में उपलब्ध होता है और runtime errors को handle करने के लिए इस्तेमाल होता है। यह java.lang.Throwable class का instance होता है।

Example:

<%@ page isErrorPage="true" %>
<p>Error Message: <%= exception.getMessage() %></p>

Quick Summary Table

ObjectScopePurpose
requestRequestClient data receive करना
responseResponseClient को reply भेजना
outPageOutput print करना
sessionSessionUser data store करना
applicationApplicationGlobal data store करना
configApplicationServlet configuration access करना
pageContextPageImplicit objects manage करना
pagePageCurrent JSP page represent करना
exceptionError PageError handle करना

Notes for Exam

  • JSP में total 9 implicit objects होते हैं।
  • request और response client-server communication के लिए essential हैं।
  • session user-specific data के लिए use होता है।
  • application पूरे web app में common data store करता है।
  • pageContext सभी implicit objects को manage करता है।
  • exception object केवल error page में available होता है।
  • Implicit objects JSP को Servlet के मुकाबले आसान बनाते हैं।