Read: Servlet to SELECT to JSP Display with JSTL
Servlet to SELECT to JSP Display with JSTL <c:forEach>
Servlet to SELECT to JSP Display with JSTL <c:forEach>
अगर आप Java Web Application बना रहे हैं और आपको Database से data निकालकर JSP page पर दिखाना है, तो ये process “Servlet → SELECT Query → JSP Display using JSTL <c:forEach>” बहुत important है। यह topic college exams और practical viva दोनों में frequently पूछा जाता है। इस blog में हम step-by-step समझेंगे कि data fetch करके JSP पर कैसे show किया जाता है।
Step 1: Basic Flow समझो
पूरा flow तीन main parts में divide होता है:
- Servlet: Database से connection बनाता है और data fetch करता है।
- Model / POJO Class: Data को object form में hold करता है।
- JSP Page: JSTL
<c:forEach>tag का use करके data display करता है।
इसका main purpose है data को clean और structured तरीके से JSP तक पहुँचाना ताकि presentation और logic अलग-अलग रहे। इस approach को MVC pattern भी कहा जाता है।
Step 2: Database Table Example
मान लीजिए हमारे पास एक database table है जिसका नाम student है।
| id | name | course | marks |
|---|---|---|---|
| 1 | Ravi | MCA | 82 |
| 2 | Neha | MCA | 90 |
Step 3: POJO Class बनाना (Student.java)
सबसे पहले हम एक simple Java class बनाएंगे जो student data को represent करेगी।
public class Student {
private int id;
private String name;
private String course;
private int marks;
// Constructor
public Student(int id, String name, String course, int marks) {
this.id = id;
this.name = name;
this.course = course;
this.marks = marks;
}
// Getters
public int getId() { return id; }
public String getName() { return name; }
public String getCourse() { return course; }
public int getMarks() { return marks; }
}
Step 4: Database Connectivity (DAO Class)
अब हम database से connect करने के लिए एक DAO (Data Access Object) class बनाएंगे जो student data fetch करेगा।
import java.sql.*;
import java.util.*;
public class StudentDAO {
public List<Student> getAllStudents() {
List<Student> list = new ArrayList<>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM student");
while (rs.next()) {
list.add(new Student(rs.getInt("id"), rs.getString("name"), rs.getString("course"), rs.getInt("marks")));
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
Step 5: Servlet बनाना (StudentServlet.java)
Servlet controller की तरह काम करेगा। यह DAO से data लेगा और JSP को forward करेगा।
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
import java.util.*;
public class StudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
StudentDAO dao = new StudentDAO();
List<Student> list = dao.getAllStudents();
req.setAttribute("studentList", list);
RequestDispatcher rd = req.getRequestDispatcher("showStudents.jsp");
rd.forward(req, res);
}
}
Step 6: JSP Page (showStudents.jsp)
अब हम JSP page बनाएंगे जिसमें JSTL का use करके data को table format में show करेंगे।
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Student Records</title>
</head>
<body>
<h2>Student List</h2>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>Course</th>
<th>Marks</th>
</tr>
<c:forEach var="s" items="${studentList}">
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.course}</td>
<td>${s.marks}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Step 7: web.xml Configuration
Servlet को mapping करने के लिए web.xml में entry करनी होगी।
<web-app>
<servlet>
<servlet-name>studentServlet</servlet-name>
<servlet-class>StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentServlet</servlet-name>
<url-pattern>/showStudents</url-pattern>
</servlet-mapping>
</web-app>
Step 8: Flow Working Explanation
अब देखते हैं कि यह पूरा process actual में कैसे चलता है:
- Browser से request आती है StudentServlet को।
- Servlet, DAO class के through database से data fetch करता है।
- Servlet data को request attribute में set करके JSP को forward करता है।
- JSP page JSTL
<c:forEach>tag से data को iterate करके table में show करता है।
Step 9: Advantages of Using JSTL <c:forEach>
- No need to write Java code inside JSP (clean separation of logic and view)।
- Easy iteration of list, arrays और collections।
- Readable, maintainable और exam-friendly syntax।
Step 10: Important Notes for Exams
- JSTL का use करने के लिए आपको jstl.jar और standard.jar libraries को
WEB-INF/libमें add करना होता है। - Always use MVC approach — Servlet (controller) + JSP (view) + DAO (model)।
- Data iteration के लिए
<c:forEach>सबसे efficient तरीका है। - Exam में अक्सर question आता है — “How to display data from Database in JSP using JSTL?”
- Remember:
request.setAttribute()का use data transfer के लिए होता है।
Step 11: Real-life Example Explanation
मान लो आपने college management system बनाया है। जब admin “View All Students” पर click करता है, तो Servlet database से सारे records निकालकर JSP को देता है। JSP page उन्हें clean table format में show करता है। ये पूरा flow एक real MVC-based architecture का example है।
Step 12: Error Handling Tips
- Database connection check करो — driver और URL सही होना चाहिए।
- NullPointerException से बचने के लिए हमेशा check करो कि list empty नहीं है।
- ForEach tag में item name सही होना चाहिए (case-sensitive)।
- Always close database connection after query execution।
Step 13: Key Benefits Summary
- Code modular और reusable हो जाता है।
- Maintenance आसान हो जाती है।
- JSP में readability बढ़ती है क्योंकि Java code हट जाता है।
- Perfect for college lab exams और mini-projects।
Step 14: Short Revision Notes
| Concept | Description |
|---|---|
| Servlet | Database से data fetch करता है और JSP को forward करता है |
| DAO | Database interaction logic handle करता है |
| JSP | Data display के लिए responsible है |
JSTL <c:forEach> |
Collection iterate करके data show करता है |
Step 15: Exam-Oriented Short Questions (For Practice)
- Servlet और JSP में क्या difference है?
<c:forEach>tag का use क्यों करते हैं?- Servlet से JSP में data pass करने के लिए कौन-सा method use होता है?
- DAO class का purpose क्या है?
- JSTL libraries add करने का process क्या है?
Step 16: Summary in Simple Words
इस पूरे topic में आपने सीखा कि Servlet से database data लेकर JSP page में कैसे display किया जाता है।
JSTL <c:forEach> tag इस process को बहुत simple बनाता है क्योंकि इससे आपको JSP में Java code नहीं लिखना पड़ता।
यह तरीका real projects और exam दोनों के लिए perfect है।