Feedback Form

config (ServletConfig), page (this), exception (Error Pages)

ServletConfig, page (this) और exception (Error Pages) in JSP – Complete Explanation in Hindi

अगर आप JSP (Java Server Pages) सीख रहे हैं, तो आपको तीन बहुत important concepts को अच्छे से समझना चाहिए — ServletConfig, page (this) और exception (Error Pages)। ये तीनों JSP के core part हैं और exam point of view से बहुत बार पूछे जाते हैं। आज हम इन्हें बिल्कुल simple और practical तरीके से समझेंगे ताकि आपको exam में direct लिखने लायक notes मिल जाएं।

ServletConfig in JSP

ServletConfig एक interface है जो servlet या JSP के configuration data को represent करता है। जब भी कोई servlet या JSP load होता है, तो उसके साथ एक ServletConfig object बनता है जो web.xml file से initialization parameters पढ़ता है।

Simple शब्दों में — ServletConfig का काम है servlet या JSP को initialization parameters provide करना

Use of ServletConfig

  • Web.xml से parameter values को JSP या servlet में पढ़ने के लिए।
  • Application को configure करने के लिए।
  • हर servlet के पास अपना अलग ServletConfig object होता है।

ServletConfig Methods

Method Description
getInitParameter(String name) Web.xml से किसी specific parameter की value return करता है।
getInitParameterNames() सभी initialization parameter names की enumeration देता है।
getServletContext() ServletContext object return करता है जिससे पूरे web application तक access मिलती है।
getServletName() Servlet का नाम return करता है।

Example of ServletConfig in JSP

मान लीजिए हमारे पास web.xml में एक parameter define किया गया है —

<web-app>
 <servlet>
  <servlet-name>DemoServlet</servlet-name>
  <servlet-class>Demo</servlet-class>
  <init-param>
   <param-name>developer</param-name>
   <param-value>Rider Team</param-value>
  </init-param>
 </servlet>
</web-app>

अब JSP में हम इस parameter को ऐसे access कर सकते हैं —

<%
ServletConfig config = getServletConfig();
String dev = config.getInitParameter("developer");
out.println("Developer: " + dev);
%>

Output होगा — Developer: Rider Team

Important Points

  • हर servlet या JSP के लिए अलग ServletConfig object बनता है।
  • ServletConfig केवल initialization parameters तक access देता है, runtime info तक नहीं।
  • यह servlet lifecycle में automatically create होता है।

page (this) in JSP

अब बात करते हैं JSP के एक बहुत common implicit object की — page। JSP में page implicit object current servlet instance को represent करता है। Java के terms में यह this keyword की तरह काम करता है।

यानी अगर आप JSP में page object use कर रहे हैं, तो आप JSP के current instance को refer कर रहे हैं।

Definition

page: यह JSP implicit object है जो current JSP page (servlet instance) को represent करता है।

Type

Type: java.lang.Object

Use of page (this)

  • Current JSP instance को refer करने के लिए।
  • Methods या variables को current object से call करने के लिए।
  • Java के this keyword की तरह behave करता है।

Example of page object

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

Output कुछ इस तरह होगा —

Class of this page object: org.apache.jsp.index_jsp

Note:

JSP में generally page object का direct use बहुत rare होता है, लेकिन exam में ये question अक्सर पूछा जाता है कि “What is the type and purpose of page implicit object?” तो याद रखें — इसका type Object है और ये current JSP instance को refer करता है।

exception (Error Pages) in JSP

अब आते हैं JSP के तीसरे और सबसे useful concept पर — exception (Error Pages)

जब JSP page में कोई error या exception होती है, तो by default server user को technical error दिखा देता है जो professional नहीं लगता। इसलिए JSP में Error Pages का use किया जाता है ताकि error को handle करके user-friendly message दिखाया जा सके।

What is exception implicit object?

exception JSP का एक implicit object है जो केवल error pages में available होता है। इसका type java.lang.Throwable होता है। यह object उस exception की information रखता है जो JSP page पर occur हुई है।

Steps to create Error Page in JSP

  • Main JSP page में error होने पर उसे एक special error page पर redirect किया जाता है।
  • Error page में isErrorPage="true" attribute लगाया जाता है।
  • Main JSP page में errorPage="error.jsp" attribute define किया जाता है।

Example of Error Handling in JSP

main.jsp

<%@ page errorPage="error.jsp" %>
<% int a = 10 / 0; %>

error.jsp

<%@ page isErrorPage="true" %>
<h3>Sorry! Something went wrong.</h3>
<p>Error Details: <%= exception.getMessage() %></p>

Output:

Sorry! Something went wrong.
Error Details: / by zero

Methods of exception object

Method Description
getMessage() Error message return करता है।
printStackTrace() Error trace को print करता है।
toString() Exception class और message को string format में देता है।

Key Points to Remember

  • exception object केवल error page में available होता है।
  • Main JSP page में इसे use नहीं किया जा सकता।
  • Always error.jsp file में isErrorPage="true" लगाना जरूरी है।
  • Error handling से application professional और secure बनता है।

Quick Summary Notes

Concept Description Type Available In
ServletConfig Initialization parameters को access करने के लिए use होता है। javax.servlet.ServletConfig JSP और Servlets दोनों में
page (this) Current JSP instance को represent करता है। java.lang.Object हर JSP page में
exception Error pages में occur हुई exception को represent करता है। java.lang.Throwable Only in error pages

Exam Tip:

  • JSP के implicit objects कुल 9 होते हैं — उनमें से ServletConfig, page और exception exam में बहुत बार पूछे जाते हैं।
  • Definition + Type + Example — ये तीनों चीज़ें हमेशा याद रखें।
  • Error handling question में attributes (isErrorPage और errorPage) जरूर mention करें।