Common Scriptlet Patterns: Loops, Conditionals, Database Calls
Common Scriptlet Patterns in JSP (Loops, Conditionals, Database Calls)
अगर आप JSP (Java Server Pages) सीख रहे हैं, तो Scriptlets एक बहुत ही जरूरी concept है जिसे समझना हर student के लिए important है। JSP में Scriptlet का use Java code को HTML के अंदर embed करने के लिए किया जाता है। Exam point of view से भी यह topic काफी बार पूछा जाता है, इसलिए आज हम समझेंगे — Common Scriptlet Patterns जैसे Loops, Conditionals, और Database Calls को simple हिंदी में।
Introduction to Scriptlets in JSP
JSP में Scriptlet एक ऐसा code block होता है जहाँ हम Java code directly लिख सकते हैं। यह code server पर execute होता है और result HTML page के रूप में client को दिखता है। Scriptlet tag इस तरह लिखा जाता है:
<% Java code यहाँ %>
Example के लिए अगर आप “Hello World” print करना चाहें, तो:
<% out.println("Hello World"); %>
JSP engine इस Java code को servlet में convert करता है और फिर compile कर के output देता है।
Common Scriptlet Patterns in JSP
अब बात करते हैं कुछ सबसे common और frequently used Scriptlet patterns की, जो आपके exam और practical दोनों में काम आते हैं — जैसे loops, conditionals, और database calls।
1. Looping Scriptlet Pattern
Loop का use तब किया जाता है जब हमें repetitive task perform करना हो। JSP में हम Java के सारे loops (for, while, do-while) use कर सकते हैं। चलिए example से समझते हैं।
For Loop Example
<%
for(int i=1; i<=5; i++) {
out.println("Number: " + i + "<br>");
}
%>
ऊपर का code 1 से 5 तक number print करेगा। यह pattern dynamic data display करने के लिए बहुत useful है, जैसे कि product list या student records।
While Loop Example
<%
int count = 1;
while(count <= 3) {
out.println("Count: " + count + "<br>");
count++;
}
%>
जब तक condition true रहेगी, loop execute होता रहेगा। यह pattern उन जगहों पर use होता है जहाँ iteration count पहले से fix नहीं होता।
Do-While Loop Example
<%
int i = 1;
do {
out.println("Value: " + i + "<br>");
i++;
} while(i <= 5);
%>
इस loop में code कम से कम एक बार जरूर execute होता है, चाहे condition false ही क्यों न हो।
2. Conditional Scriptlet Pattern
Conditionals का use decision making के लिए होता है। JSP में हम if, if-else, else-if ladder और switch statements को use कर सकते हैं।
If-Else Pattern
<%
int marks = 75;
if(marks >= 80) {
out.println("Grade: A");
} else if(marks >= 60) {
out.println("Grade: B");
} else {
out.println("Grade: C");
}
%>
इस pattern से हम multiple conditions handle कर सकते हैं, जैसे कि student result evaluation या login authentication।
Switch Case Pattern
<%
int day = 3;
switch(day) {
case 1: out.println("Monday"); break;
case 2: out.println("Tuesday"); break;
case 3: out.println("Wednesday"); break;
default: out.println("Invalid Day");
}
%>
Switch statement तब useful होती है जब हमें कई discrete values पर decision लेना होता है।
3. Database Call Scriptlet Pattern
अब आते हैं सबसे important और real-world में used pattern पर — Database Call। JSP में database से data fetch करने के लिए JDBC (Java Database Connectivity) का use किया जाता है। यह concept हर semester exam और mini project में पूछा जाता है।
Database Connection Pattern
सबसे पहले database से connect करने के लिए driver load करना पड़ता है, फिर connection establish करना होता है। नीचे एक simple example है:
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM students");
while(rs.next()) {
out.println("Name: " + rs.getString("name") + "<br>");
}
con.close();
} catch(Exception e) {
out.println("Error: " + e.getMessage());
}
%>
यह scriptlet database से data read करके web page पर dynamically display करती है। यह pattern हर web application का core हिस्सा होता है।
Insert Operation Pattern
<%
String name = request.getParameter("name");
String email = request.getParameter("email");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
PreparedStatement ps = con.prepareStatement("INSERT INTO students(name, email) VALUES(?, ?)");
ps.setString(1, name);
ps.setString(2, email);
ps.executeUpdate();
out.println("Record Inserted Successfully!");
con.close();
} catch(Exception e) {
out.println("Error: " + e.getMessage());
}
%>
यह pattern user input को database में store करता है। इसे “Form Handling” pattern भी कहा जाता है।
Update Operation Pattern
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
PreparedStatement ps = con.prepareStatement("UPDATE students SET name=? WHERE id=?");
ps.setString(1, name);
ps.setString(2, id);
ps.executeUpdate();
out.println("Record Updated Successfully!");
con.close();
} catch(Exception e) {
out.println("Error: " + e.getMessage());
}
%>
यह pattern database records को modify करने के लिए उपयोग होता है, जैसे student details update करना।
Delete Operation Pattern
<%
String id = request.getParameter("id");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
PreparedStatement ps = con.prepareStatement("DELETE FROM students WHERE id=?");
ps.setString(1, id);
ps.executeUpdate();
out.println("Record Deleted Successfully!");
con.close();
} catch(Exception e) {
out.println("Error: " + e.getMessage());
}
%>
इस pattern का use unwanted data को delete करने के लिए किया जाता है।
4. Best Practices for Using Scriptlets
- Business logic को JSP से अलग रखना चाहिए — use Java Beans या Servlets।
- Scriptlet को कम से कम use करें और ज़्यादा logic JSTL या EL (Expression Language) में रखें।
- Connection को हमेशा close करें ताकि memory leak न हो।
- PreparedStatement का use करें ताकि SQL Injection से बचा जा सके।
Exam Notes Summary
| Pattern | Purpose | Example Use Case |
|---|---|---|
| Loop Scriptlet | Repetition या iteration | List of Students |
| Conditional Scriptlet | Decision Making | Grade Calculation |
| Database Call Scriptlet | Data Fetch/Store | Dynamic Web Application |
तो दोस्तों, ये थे JSP के सबसे important और commonly used Scriptlet Patterns — Loops, Conditionals, और Database Calls। इन्हें अच्छे से practice करें क्योंकि ये not only exam में बल्कि real web development projects में भी बहुत काम आते हैं।