Feedback Form

Introduction to Directive: Page-Level Settings

Introduction to <%@ page %> Directive: Page-Level Settings

JSP (Java Server Pages) में <%@ page %> directive सबसे important directive में से एक है। यह directive page-level settings को define करने के लिए use होती है। यानी यह बताती है कि JSP page को कैसे execute करना है, कौन सी language use होगी, कौन सा error page होगा, कौन सा buffer size रहेगा आदि। यह directive पूरे JSP page के behavior को control करती है।

What is <%@ page %> Directive?

<%@ page %> directive JSP में एक page-विशिष्ट instruction होती है जो JSP engine को बताती है कि page को कैसे compile और execute करना है। यह compile-time पर काम करती है यानी जब JSP को Servlet में convert किया जाता है तब इसके attributes को read किया जाता है।

Example:

<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>

ऊपर दिए गए example में, language, contentType और pageEncoding attributes define करते हैं कि JSP file किस भाषा में लिखी गई है, browser को किस type का content भेजना है और JSP file किस encoding में है।

Syntax of <%@ page %> Directive

<%@ page attribute="value" attribute="value" ... %>

इस directive में कई attributes होते हैं, और हर attribute का एक specific purpose होता है। इन्हें comma से नहीं बल्कि space से separate किया जाता है।

List of Attributes of <%@ page %> Directive

नीचे सभी important attributes और उनके use explain किए गए हैं:

Attribute Description Example
language यह बताता है कि JSP किस programming language में लिखा गया है। Default language Java होती है। language="java"
extends यह specify करता है कि JSP से बने Servlet किस class को extend करेगा। extends="mypackage.MyServlet"
import यह Java packages को import करने के लिए use होता है, जैसे Java में import statement। import="java.util.*,java.sql.*"
session यह बताता है कि JSP page में session implicit object available होगा या नहीं। session="true"
buffer यह JSP output के लिए buffer size set करता है। Default value "8kb" होती है। buffer="16kb"
autoFlush यह बताता है कि buffer full होने पर auto flush होगा या exception throw होगा। autoFlush="true"
isThreadSafe यह बताता है कि JSP page thread-safe है या नहीं। अगर false है तो JSP को single-threaded mode में चलाया जाता है। isThreadSafe="true"
info JSP page की information provide करने के लिए use किया जाता है। info="This page shows student details"
errorPage अगर page पर कोई error आती है तो यह attribute बताता है कि किस JSP page को error handling के लिए call करना है। errorPage="error.jsp"
isErrorPage अगर यह page किसी error को handle करता है, तो इसे true set किया जाता है। isErrorPage="true"
contentType यह बताता है कि browser को कौन सा MIME type content भेजना है। contentType="text/html"
pageEncoding यह JSP file की character encoding define करता है। pageEncoding="UTF-8"
isELIgnored यह बताता है कि Expression Language (EL) को ignore करना है या नहीं। isELIgnored="false"
deferredSyntaxAllowedAsLiteral यह बताता है कि EL syntax को literal value के रूप में allow करना है या नहीं। deferredSyntaxAllowedAsLiteral="true"

Important Attributes Explained in Detail

1. language attribute

यह JSP page में use होने वाली programming language को define करता है। Default value हमेशा java होती है।

<%@ page language="java" %>

इस directive के बिना JSP automatically Java language को मान लेता है।

2. import attribute

यह attribute Java packages को import करने के लिए use किया जाता है, ताकि आप JSP में उन classes का direct use कर सकें।

<%@ page import="java.util.Date, java.sql.Connection" %>

इसका use Java के import statement जैसा ही होता है।

3. session attribute

अगर आप JSP में session implicit object का use करना चाहते हैं तो इसे true set करें। अगर आपको session की जरूरत नहीं है, तो performance improve करने के लिए इसे false कर सकते हैं।

<%@ page session="false" %>

4. errorPage और isErrorPage attributes

ये दोनों attributes error handling के लिए use किए जाते हैं। अगर किसी JSP page पर error आती है तो errorPage attribute उस JSP page को redirect करता है जो error को handle करता है। और उस error handling page में isErrorPage="true" होना चाहिए।

Example:

<%@ page errorPage="error.jsp" %> <%@ page isErrorPage="true" %>

इससे कोई भी runtime error होने पर control error.jsp पर चला जाएगा।

5. contentType और pageEncoding

contentType बताता है कि JSP किस प्रकार का data output करेगा — जैसे text/html, text/plain, या application/json। जबकि pageEncoding बताता है कि JSP file किस encoding format में save और execute होगी।

<%@ page contentType="text/html" pageEncoding="UTF-8" %>

6. buffer और autoFlush attributes

buffer attribute JSP output buffer का size तय करता है। जब output buffer full हो जाता है, तब autoFlush तय करता है कि data automatically flush होगा या exception throw होगा।

<%@ page buffer="16kb" autoFlush="true" %>

अगर autoFlush="false" set करते हैं, तो buffer full होने पर exception आएगा।

7. isThreadSafe attribute

यह बताता है कि JSP page एक time में multiple threads handle कर सकता है या नहीं। अगर इसे false set किया जाता है, तो JSP container इसे single-threaded बनाता है।

<%@ page isThreadSafe="false" %>

इससे performance थोड़ा कम हो जाता है, लेकिन thread safety बढ़ जाती है।

Multiple Attributes in One Directive

आप एक ही <%@ page %> directive में multiple attributes use कर सकते हैं।

<%@ page language="java" import="java.util.*,java.sql.*" contentType="text/html" pageEncoding="UTF-8" session="true" %>

इससे page का behavior और environment पूरी तरह control में रहता है।

Example Program using <%@ page %> Directive

नीचे एक simple JSP program दिया गया है जो page directive के use को practically दिखाता है:

<%@ page language="java" contentType="text/html" pageEncoding="UTF-8" import="java.util.Date" %>
<html>
<body>
<h3>Welcome to JSP Page Directive Example</h3>
<p>Current Date and Time: <%= new Date() %></p>
</body>
</html>

इस code में हमने import attribute से java.util.Date class को import किया है और contentTypepageEncoding से output format define किया है।

Importance of <%@ page %> Directive in JSP

  • यह JSP page के behavior को control करता है।
  • Compile-time पर environment set करने में मदद करता है।
  • Error handling, session management और threading control देता है।
  • Performance और flexibility दोनों improve करता है।
  • Code readability और maintainability बढ़ाता है।

Exam Point of View (Notes)

  • Definition: <%@ page %> directive JSP page-level settings define करती है।
  • Syntax: <%@ page attribute="value" %>
  • Attributes: language, import, session, errorPage, isErrorPage, contentType, buffer, autoFlush, isThreadSafe, pageEncoding आदि।
  • Default Values: language="java", session="true", buffer="8kb", autoFlush="true"
  • Use: JSP page के compile-time behavior को control करने के लिए।
  • Example: <%@ page contentType="text/html" pageEncoding="UTF-8" %>

Key Points to Remember

  • यह directive हमेशा JSP file के शुरुआत में use होती है।
  • एक JSP file में multiple page directives हो सकते हैं।
  • हर directive compile-time पर execute होती है।
  • यह developer को JSP page के हर aspect पर control देती है।
  • Exam में अक्सर पूछा जाता है — “Explain any five attributes of page directive with example.”