Basic Structure of JSP Code File: Syntax
Basic Structure of JSP Code File: Syntax
Introduction to JSP Structure
JSP यानी Java Server Pages, एक ऐसी technology है जो dynamic web pages create करने के लिए use होती है। JSP में HTML और Java code दोनों को mix किया जा सकता है। यानी हम HTML से page design करते हैं और Java से logic add करते हैं।
JSP का main purpose होता है server-side processing करना — जैसे database से data लाना, form submit करना या dynamic content generate करना। लेकिन ये सब काम HTML page के अंदर ही Java code embed करके किया जाता है।
JSP Basic Syntax
JSP file का basic structure बहुत simple होता है। JSP file को generally .jsp extension से save किया जाता है। एक JSP file के अंदर HTML code, Java code और JSP directives तीनों का combination होता है।
JSP File Structure Example
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Basic JSP Structure</title>
</head>
<body>
<h2>Welcome to JSP</h2>
<%
String name = "Student";
out.println("Hello, " + name + "! Welcome to JSP World.");
%>
</body>
</html>
ऊपर दिए गए example में HTML और Java code दोनों को एक ही page में लिखा गया है। JSP compiler इस code को internally Servlet में convert करता है, और फिर run होने पर browser को HTML output देता है।
JSP File Components
एक JSP file मुख्य रूप से चार parts में divided होती है — Directives, Scriptlets, Expressions, और Declarations। चलिए इन्हें एक-एक करके समझते हैं।
1. JSP Directives
Directives JSP container को instructions देती हैं कि page को कैसे process करना है। ये हमेशा <%@ ... %> के अंदर लिखी जाती हैं।
मुख्यतः तीन types की directives होती हैं:
- page directive — page की properties define करती है।
- include directive — किसी external file को include करती है।
- taglib directive — custom tags use करने के लिए होती है।
Example:
<%@ page language="java" contentType="text/html" %>
<%@ include file="header.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
2. JSP Scriptlets
Scriptlet tag के अंदर Java code लिखा जाता है। इसे JSP engine translate करता है और Servlet class के अंदर डालता है।
Syntax:
<% Java code %>
Example:
<%
int a = 10;
int b = 20;
int sum = a + b;
out.println("Sum = " + sum);
%>
3. JSP Expressions
Expression tag का use किसी value को directly HTML output में print करने के लिए किया जाता है।
Syntax:
<%= expression %>
Example:
<%= "Welcome to JSP" %>
ये tag internally out.print() function call करता है।
4. JSP Declarations
Declaration tag में हम variables और methods declare करते हैं जिन्हें पूरा page use कर सकता है।
Syntax:
<%! declaration code %>
Example:
<%!
int counter = 0;
public int getSquare(int n) {
return n * n;
}
%>
अब ये declared methods या variables पूरे JSP page में accessible होते हैं।
JSP Comments
JSP में दो तरह के comments होते हैं — HTML comment और JSP comment।
| Type | Syntax | Visible to Client |
|---|---|---|
| HTML Comment | <!-- comment --> |
Yes |
| JSP Comment | <%-- comment --%> |
No |
JSP comments browser में दिखाई नहीं देते क्योंकि वो server-side पर ही remove हो जाते हैं।
Important JSP Directives Explained
1. Page Directive
Page directive JSP page की overall configuration control करती है — जैसे language, buffer size, error page आदि।
Example:
<%@ page language="java" contentType="text/html; charset=UTF-8" errorPage="error.jsp" %>
| Attribute | Description |
|---|---|
| language | JSP में use की जाने वाली language (usually Java) |
| contentType | Response का MIME type set करता है |
| errorPage | Error आने पर redirect होने वाली page को define करता है |
| isErrorPage | बताता है कि यह page error page है या नहीं |
2. Include Directive
Include directive static content को एक JSP file से दूसरी में include करने के लिए use होता है।
Syntax:
<%@ include file="filename.jsp" %>
3. Taglib Directive
Taglib directive custom tags या JSTL (JSP Standard Tag Library) को include करने के लिए होती है।
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSP Lifecycle and Compilation
जब JSP page run होता है, तो internally यह Servlet में convert हो जाता है। इसका lifecycle कुछ इस प्रकार होता है:
- JSP file को Servlet में translate किया जाता है।
- Servlet को compile किया जाता है।
- Servlet class load होकर initialize होती है।
- Request process होता है और response generate होता है।
JSP Lifecycle Methods
| Method | Description |
|---|---|
jspInit() |
JSP के initialize होने पर call होता है। |
_jspService() |
हर request पर call होता है। |
jspDestroy() |
JSP बंद होने से पहले call होता है। |
Practical Example of JSP File
अब देखते हैं कि एक simple JSP file में कैसे logic और HTML दोनों का use किया जाता है।
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP Example</title>
</head>
<body>
<h2>Student Marks Example</h2>
<%
int marks = 85;
if(marks >= 90){
out.println("Grade: A+");
} else if(marks >= 75){
out.println("Grade: A");
} else {
out.println("Grade: B");
}
%>
</body>
</html>
इस example में Java logic के ज़रिए student का grade calculate किया गया है और HTML page पर show किया गया है।
Advantages of Proper JSP Structure
- Code readability और maintenance आसान होता है।
- Java और HTML दोनों को अलग-अलग handle किया जा सकता है।
- Dynamic content generation possible होता है।
- Reusable components और modular design बनता है।
Best Practices for Writing JSP Code
- JSP में ज्यादा logic ना रखें — इसे Servlet या Bean में shift करें।
- JSTL और EL (Expression Language) का use करें।
- HTML और Java code को अलग-अलग रखें ताकि debugging आसान हो।
- Proper comments और indentation maintain करें।
JSP Syntax Summary Table
| Component | Tag Syntax | Purpose |
|---|---|---|
| Directive | <%@ ... %> |
JSP instructions define करता है। |
| Declaration | <%! ... %> |
Variables या methods declare करने के लिए। |
| Scriptlet | <% ... %> |
Java code execute करने के लिए। |
| Expression | <%= ... %> |
Value को directly output करने के लिए। |
Where JSP Structure is Used
JSP का use कई जगह होता है, जैसे:
- Dynamic web pages बनाना
- Form data process करना
- Database interaction के लिए
- Session management और cookies handling
Key Points to Remember
- JSP file हमेशा
.jspextension से save होती है। - Server इसे Servlet में convert करता है।
- Java code को
<% ... %>tags में लिखा जाता है। - HTML और JSP दोनों एक साथ use किए जा सकते हैं।
- Proper structure से performance और readability बेहतर होती है।