0% found this document useful (0 votes)
2 views7 pages

Java Completed

The document outlines a complete example of a web application using JDBC, Servlets, and JSP/JSTL for managing student data. It includes project structure, prerequisites, model (Student.java), DAO (StudentDAO.java), servlet (StudentListServlet.java), JSP view (students.jsp), and optional web.xml configuration. The application allows users to view a list of students by accessing a specific URL, with data retrieved from a MySQL database and displayed in an HTML table.

Uploaded by

fportor994
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Java Completed

The document outlines a complete example of a web application using JDBC, Servlets, and JSP/JSTL for managing student data. It includes project structure, prerequisites, model (Student.java), DAO (StudentDAO.java), servlet (StudentListServlet.java), JSP view (students.jsp), and optional web.xml configuration. The application allows users to view a list of students by accessing a specific URL, with data retrieved from a MySQL database and displayed in an HTML table.

Uploaded by

fportor994
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Exemple Complet (jdbc+servelet+jsp/jstl)

1. Structure dyal l’projet (f Eclipse - Dynamic Web Project)


text
GestionEtudiants/
├── src/
│ └── [Link]/
│ ├── model/
│ │ └── [Link]
│ ├── dao/
│ │ └── [Link]
│ └── servlet/
│ └── [Link]
├── WebContent/
│ ├── WEB-INF/
│ │ ├── lib/ ← (hna dir [Link] + [Link])
│ │ ├── [Link]
│ │ └── classes/
│ ├── [Link]
│ └── [Link]
├── build/
└── WebContent/WEB-INF/[Link]

Prérequis (ghadi tcherhihom l’dlari) :


• Tomcat 10+ (ila Tomcat 9 dir javax m3a [Link]).
• Ajoute 2 JARs f WEB-INF/lib :
• [Link] (pour JDBC)
• [Link] + [Link] (pour JSTL)

2. Model : [Link] (POJO)


Java
package [Link];

public class Student {


private int id;
private String nom;
private int age;
private String filiere;

// Constructeur vide (obligatoire pour JSTL)


public Student() {}

// Constructeur avec paramètres


public Student(int id, String nom, int age, String filiere) {
[Link] = id;
[Link] = nom;
[Link] = age;
[Link] = filiere;
}

// Getters et Setters (importants bach JSTL y9der y9ra ${[Link]})


public int getId() { return id; }
public void setId(int id) { [Link] = id; }

public String getNom() { return nom; }


public void setNom(String nom) { [Link] = nom; }

public int getAge() { return age; }


public void setAge(int age) { [Link] = age; }

public String getFiliere() { return filiere; }


public void setFiliere(String filiere) { [Link] = filiere; }
}

3. DAO : [Link] (JDBC + ArrayList)


Java
package [Link];

import [Link];
import [Link].*;
import [Link];
import [Link];

public class StudentDAO {

private static final String URL = "jdbc:mysql://localhost:3306/ecole?


useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASS = "";

// Méthode principale : katred ArrayList<Student> mn DB


public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>(); // Hna ArrayList bach n7etto ga3 données

String sql = "SELECT * FROM etudiants ORDER BY id";

try (Connection conn = [Link](URL, USER, PASS);


Statement stmt = [Link]();
ResultSet rs = [Link](sql)) {

while ([Link]()) {
Student s = new Student();
[Link]([Link]("id"));
[Link]([Link]("nom"));
[Link]([Link]("age"));
[Link]([Link]("filiere"));

[Link](s); // Ajout f l’ArrayList


}

[Link]("✅ " + [Link]() + " étudiants chargés depuis la DB.");

} catch (SQLException e) {
[Link]("❌ Erreur JDBC dans DAO : " + [Link]());
[Link]();
}
return students; // Retourne l’ArrayList
}
}

4. Servlet : [Link] (Controller)


Java
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

// URL : [Link]
@WebServlet("/liste-etudiants")
public class StudentListServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


private StudentDAO studentDAO = new StudentDAO();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Étape 1 : Appel DAO pour récupérer ArrayList mn DB via JDBC


List<Student> students = [Link]();

// Étape 2 : Mettre l’ArrayList f l’objet request (bach JSP t9der t9raha)


[Link]("etudiants", students);
// Étape 3 : Forward vers JSP (View)
[Link]("/[Link]").forward(request, response);

[Link]("📤 Servlet a forwardé " + [Link]() + " étudiants vers JSP.");


}
}

5. View : [Link] (JSP + JSTL)


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="[Link]" %> <!-- JSTL Core -->

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Liste des Étudiants</title>
<style>
table { border-collapse: collapse; width: 80%; margin: 20px auto; }
th, td { border: 1px solid #333; padding: 10px; text-align: center; }
th { background-color: #4CAF50; color: white; }
</style>
</head>
<body>
<h1 style="text-align:center;">📋 Liste des Étudiants (JDBC + Servlet + JSTL)</h1>

<table>
<tr>
<th>ID</th>
<th>Nom</th>
<th>Âge</th>
<th>Filière</th>
</tr>

<!-- Boucle JSTL sur l’ArrayList "etudiants" li ja mn Servlet -->


<c:forEach var="etudiant" items="${etudiants}">
<tr>
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>${[Link]}</td>
</tr>
</c:forEach>
</table>

<c:if test="${empty etudiants}">


<p style="text-align:center; color:red;">Aucun étudiant trouvé dans la base de données.</p>
</c:if>

<p style="text-align:center;">
<a href="liste-etudiants">Rafraîchir la liste</a>
</p>
</body>
</html>

6. [Link] (facultatif – on utilise @WebServlet)


XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
version="6.0">
<display-name>GestionEtudiants</display-name>
</web-app>

Explication complète hors code (b darija + français – bach tcherh f l’class b soror)
Chno dar l’application ?
1. L’user kaykteb f navigateur : [Link]
2. Servlet (StudentListServlet) kattt7el (doGet).
3. Servlet kat3ayti [Link]() → JDBC kayconnecti l’base, kaydir SELECT *,
w kay7ell chaque ligne f objet Student w kay7etto f ArrayList.
4. Servlet kat7ett l’ArrayList f [Link]("etudiants", students) (hada hwa l’pont bin
Controller w View).
5. Servlet katforwerdi l’requête l’[Link].
6. JSP kat9ra l’ArrayList m3a <c:forEach var="etudiant" items="${etudiants}"> (JSTL kat3awen
bach ma nkteboch Java code f JSP).
7. Kol ligne f DB katban f tableau HTML.

You might also like