Introduction to JSP File Anatomy: Directives, Scriptlets, EL, Actions
Introduction to JSP File Anatomy: Directives, Scriptlets, EL, Actions
जब हम Java Server Pages (JSP) सीखते हैं, तो एक बहुत जरूरी concept होता है — JSP File Anatomy यानी एक JSP file के अंदर क्या-क्या parts होते हैं और उनका use कैसे किया जाता है। अगर आप web development या Java-based dynamic websites बना रहे हैं, तो ये concept समझना बहुत जरूरी है क्योंकि इसी से आपका page server पर execute होता है और browser में final HTML output दिखाता है।
JSP file असल में एक HTML page ही होता है जिसमें Java code, JSP tags और directives को embed किया जाता है ताकि dynamic content generate किया जा सके। JSP file को server-side पर Servlet में convert किया जाता है और फिर user के browser में output भेजा जाता है।
JSP Directives
JSP directives वो instructions होती हैं जो JSP container को बताती हैं कि page को कैसे process करना है। इन्हें compile-time पर interpret किया जाता है, यानी जब JSP page Servlet में convert होता है। Directive हमेशा <%@ ... %> के अंदर लिखी जाती है।
Directives के तीन main types होते हैं:
- Page Directive
- Include Directive
- Taglib Directive
1. Page Directive
Page directive पूरे JSP page के behavior को define करता है। इसमें contentType, import, errorPage, session आदि properties set की जाती हैं।
Syntax:
<%@ page attribute="value" %>
Example:
<%@ page contentType="text/html" language="java" import="java.util.*" errorPage="error.jsp" %>
ऊपर के example में हमने JSP को बताया है कि page का content type HTML है, language Java है, और error आने पर "error.jsp" page open हो।
| Attribute | Description |
|---|---|
| language | JSP file में use की जाने वाली language (default: java) |
| contentType | Response का MIME type बताता है |
| import | Java packages import करने के लिए |
| errorPage | Error होने पर जिस page पर redirect करना है |
| isErrorPage | बताता है कि page error handle कर सकता है या नहीं |
| session | Session object available है या नहीं (true/false) |
2. Include Directive
Include directive किसी external file को compile-time पर JSP में include करता है। यह code reusability के लिए useful होता है, जैसे header या footer pages।
Syntax:
<%@ include file="filename.jsp" %>
Example:
<%@ include file="header.jsp" %>
<h2>Welcome to Main Page</h2>
<%@ include file="footer.jsp" %>
यहाँ header.jsp और footer.jsp के content को main page में जोड़ दिया जाएगा।
3. Taglib Directive
Taglib directive का use तब किया जाता है जब हम custom tags या JSTL (JSP Standard Tag Library) का use करते हैं। यह directive JSP को बताता है कि कौन-सा tag library किस prefix से use होगी।
Syntax:
<%@ taglib uri="uri-of-library" prefix="prefixName" %>
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${message}" />
JSP Scriptlets
Scriptlets JSP का वो हिस्सा है जहाँ आप directly Java code लिख सकते हैं। ये code Servlet के service() method के अंदर execute होता है। Scriptlet हमेशा <% ... %> टैग के अंदर लिखा जाता है।
Syntax:
<% Java Code %>
Example:
<%
String name = "Student";
out.println("Welcome " + name);
%>
ऊपर के example में हमने JSP के अंदर Java variable declare किया और उसे output में print किया। लेकिन ध्यान रखें — ज्यादा Java code JSP में लिखने से readability कम हो जाती है, इसलिए logic को Servlet या backend file में रखना बेहतर होता है।
Declaration Element
Declaration element में हम methods या variables declare करते हैं जो पूरे JSP page में accessible होते हैं। ये code Servlet class के बाहर generate होता है।
Syntax:
<%! Java Declaration %>
Example:
<%!
int counter = 0;
public int getCount() {
return ++counter;
}
%>
<%= getCount() %>
यहाँ हर बार page reload होने पर counter increment होगा और display होगा।
Expression Element
Expression element का use किसी Java expression का output directly browser में दिखाने के लिए किया जाता है। ये out.print() की तरह काम करता है।
Syntax:
<%= expression %>
Example:
<%= new java.util.Date() %>
यह current date और time दिखाएगा।
JSP Expression Language (EL)
Expression Language (EL) JSP का modern feature है जो JSP pages में Java code की dependency को कम करता है। EL के through हम request, session, application scope से data easily access कर सकते हैं बिना Java syntax के।
Syntax:
${expression}
Example:
${userName}
अगर “userName” variable session या request scope में defined है, तो उसका value automatically show होगा।
EL Scopes
EL के अंदर data चार scopes में search किया जाता है:
- pageScope – Current page के लिए data
- requestScope – Current request के लिए data
- sessionScope – Current session के लिए data
- applicationScope – पूरे application के लिए data
Example:
${sessionScope.user}
यह session object में stored “user” attribute को access करेगा।
EL Operators
| Operator | Use |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / or div | Division |
| % or mod | Modulus |
| == or eq | Equal comparison |
| != or ne | Not equal comparison |
| && or and | Logical AND |
| || or or | Logical OR |
Example:
${5 + 3} → Output: 8
EL expressions automatically data types को handle करते हैं, इसलिए casting या conversion की जरूरत नहीं पड़ती।
JSP Actions
JSP Action tags XML-style tags होते हैं जो dynamic behavior जोड़ते हैं और run-time पर execute होते हैं। ये tags JavaBeans को manipulate करने, file include करने, forward करने आदि के लिए use किए जाते हैं।
Action tags हमेशा <jsp:actionname> के format में लिखे जाते हैं।
Common JSP Action Tags
| Tag | Description |
|---|---|
| <jsp:include> | Run-time पर किसी file को include करता है |
| <jsp:forward> | Request को दूसरे page पर forward करता है |
| <jsp:useBean> | JavaBean को declare और instantiate करता है |
| <jsp:setProperty> | Bean property का value set करता है |
| <jsp:getProperty> | Bean property का value get करता है |
Example of jsp:useBean
<jsp:useBean id="user" class="com.example.User" scope="session" />
<jsp:setProperty name="user" property="userName" value="Ravi" />
<jsp:getProperty name="user" property="userName" />
ऊपर दिए गए example में हमने एक JavaBean “User” को session scope में use किया और उसके property values को set और get किया।
jsp:include vs include Directive
| Feature | jsp:include | Include Directive |
|---|---|---|
| Execution Time | Run-time | Compile-time |
| File Update Effect | Instant reflect | Recompile needed |
| Use | Dynamic include | Static include |
Summary
JSP File Anatomy में चार मुख्य parts होते हैं — Directives, Scriptlets, Expression Language (EL) और Actions। इनका सही use करना जरूरी है ताकि आपका JSP page efficient, readable और maintainable रहे।
- Directives compile-time instructions देती हैं।
- Scriptlets runtime Java code लिखने की सुविधा देते हैं।
- EL Java code को simplify करता है और readability बढ़ाता है।
- Action tags run-time behavior control करते हैं।
अगर आप college exams या practicals की तैयारी कर रहे हैं, तो याद रखें — JSP में हमेशा logic को Servlet या backend में रखें और JSP file को presentation layer तक सीमित रखें। इससे code clean और professional बनता है।