Bonsoir,
Je suis débutant et pour mon exercice je travaille sur un projet de gestion de produits.
Mais voilà, je n'arrive pas à afficher les données de la base MySQL. J'ai comme serveur Tomcat 10.1.
La page s'affiche mais aucune donnée.
Fichier JSP
Code de connexion à la base
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>LISTE DES PRODUITS</h1> <table border="1"> <tr> <th>DESIGNATION</th> <th>PRIX</th> <th>POIDS</th> <th>DATE PEREMPTION</th> </tr> <c:forEach var="produit" items="${produits}"> <tr> <td><c:out value="${produit.designation}" /></td> <td><c:out value="${produit.prix}" /></td> <td><c:out value="${produit.poids}" /></td> <td><c:out value="${produit.date}" /></td> </tr> </c:forEach> </table> </body> </html>
Mon fichier bean Produit
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public class Singleton { private static Connection connection; static { try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/gestionproduit", "root",""); } catch(SQLException | ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() { return connection; } }
Fichier Produit qui implémente l'interface Produit
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class ProduitBean { private int id; private String designation ; private Double prix; private Double poids; private String datePeremption; public ProduitBean() { super(); } public ProduitBean(String designation, Double prix, Double poids, String dateperemption) { this.designation = designation; this.prix = prix; this.poids = poids; this.datePeremption = dateperemption; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public Double getPrix() { return prix; } public void setPrix(Double prix) { this.prix = prix; } public Double getPoids() { return poids; } public void setPoids(Double poids) { this.poids = (double) poids; } public String getDatePeremption() { return datePeremption; } public void setDatePeremption(String date) { this.datePeremption = date; } }
Ma Servlet
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class C_Produit implements I_Produit{ Connection cn = null; PreparedStatement ps = null; ResultSet rs = null; public List<ProduitBean> getListeProduit() { List<ProduitBean> produits = new ArrayList<ProduitBean>(); try { cn = Singleton.getConnection(); ps = cn.prepareStatement("SELECT * FROM produit"); rs = ps.executeQuery(); while(rs.next()) { String designation = rs.getString("designation"); Double prix = rs.getDouble("prix"); Double poids = rs.getDouble("poids"); String date = rs.getString("date"); ProduitBean ListProduit = new ProduitBean(); ListProduit.setDesignation(designation); ListProduit.setPrix(prix); ListProduit.setPoids(poids); ListProduit.setDatePeremption(date); produits.add(ListProduit); } } catch(SQLException e) { e.printStackTrace(); } return produits; } }
Fichier web.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 @WebServlet("/ProduitServlet") public class ProduitServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ProduitServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { C_Produit listProduit = new C_Produit(); request.setAttribute("produits", listProduit.getListeProduit()); this.getServletContext().getRequestDispatcher( "/liste.jsp" ).forward( request, response ); } protected void doPost(HttpServletRequest request, ServletResponse response) throws ServletException, IOException { } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ProduitServlet</display-name> <servlet> <servlet-name>ProduitServlet</servlet-name> <servlet-class>com.controler.ProduitServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ProduitServlet</servlet-name> <url-pattern>/liste</url-pattern> </servlet-mapping> </web-app>
Partager