Bonjour,

Je veux créer une application web qui permet de présenter le catalogue de produits d’une entreprise, donc voici les fichiers des différentes couches:

Le modèle
-Classe Categorie.java
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
 
package mod.catal;
public class Categorie {
	private Long idCat;
	private String titre;
	private String description;
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Categorie(){	
	}
	public Long getIdCat() {
		return idCat;
	}
	public void setIdCat(Long idCat) {
		this.idCat = idCat;
	}
	public String getTitre() {
		return titre;
	}
	public void setTitre(String titre) {
		this.titre = titre;
	}
}
- Classe Produit.java

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
 package mod.catal;
 
public class Produit {
	private Long idProduit;
	private String designation;
	private boolean selected;
	private double prix;
	private String quantite;
	private String photo;
	private Categorie categorie = new Categorie();
 
	public Produit(){
 
	}
	public Categorie getCategorie() {
		return categorie;
	}
	public void setCategorie(Categorie categorie) {
		this.categorie = categorie;
	}
	public Long getIdProduit() {
		return idProduit;
	}
	public void setIdProduit(Long idProduit) {
		this.idProduit = idProduit;
	}
	public String getDesignation() {
		return designation;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	public String getPhoto() {
		return photo;
	}
	public void setPhoto(String photo) {
		this.photo = photo;
	}
	public boolean isSelected() {
		return selected;
	}
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	public double getPrix() {
		return prix;
	}
	public void setPrix(double prix) {
		this.prix = prix;
	}
	public String getQuantite() {
		return quantite;
	}
public void setQuantite(String quantite) {
		this.quantite = quantite;
	}
}
- Classe Utilitaire.java

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
 package mod.catal;
import java.sql.*;
public class Utilitaire {
	private static Connection conn;
	static{
		try{
		 Class.forName("com.mysql.jdbc.Driver");
		 conn=DriverManager.getConnection("jdbc:mysql://localhost:8080/Categories","root","");
		}
		catch(Exception e){
			e.printStackTrace();
		}
 
	}
	public static Connection getConnection(){
		return conn;
	}
 
}


- Classe Catalogue.java

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
59
60
61
62
63
64
65
66
67
68
69
70
71
 package mod.catal;
 
import java.sql.*;
import java.util.Iterator;
import java.util.Vector;
 
public class Catalogue {
 
	public Catalogue(){
 
	}
	public void addCategorie(String titreCat,String description){
		Connection conn=Utilitaire.getConnection();
		String req="insert into categories(NOM_CAT,DESCRIPTION) values(?,?)";
		try {
			PreparedStatement ps=conn.prepareStatement(req);
			ps.setString(1,titreCat);
			ps.setString(2,description);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}
public void DelCategorie(long idCat){
		Connection conn=Utilitaire.getConnection();
		String req="delete from categories where ID_CAT=?";
		try {
			PreparedStatement ps=conn.prepareStatement(req);
			ps.setLong(1,idCat);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}
	public Vector selectCatAll(){
		String requete="select * from categories";
		return selectCategories(requete);
	}
	public Vector selectCatByKeyword(String keyword){
		String requete="select * from categories where NOM_CAT like '%"+keyword+"%'";
		return selectCategories(requete);
	}
	private Vector selectCategories(String requete){
		Connection conn=Utilitaire.getConnection();
		Vector lesCat=new Vector();
		Categorie cat=null;
		try {
			PreparedStatement ps=conn.prepareStatement(requete);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				cat=new Categorie();
				cat.setIdCat(new Long(rs.getLong("ID_CAT")));
				cat.setTitre(rs.getString("NOM_CAT"));
				cat.setDescription(rs.getString("DESCRIPTION"));
				lesCat.add(cat);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return lesCat;
	}
 
	public static void main(String[] args) {
		Catalogue catal=new Catalogue();
		Iterator lesCat=catal.selectCatAll().iterator();
		while(lesCat.hasNext()){
			Categorie cat=(Categorie)lesCat.next();
			System.out.println(cat.getTitre());
		}
	}
}
Couche de présentation

CategorieForm.java

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
 package pres.beans;
 
import java.util.*;
 
public class CategorieForm {
	private long idCat;
	private String motCle="";
	public String getMotCle() {
		return motCle;
	}
	public void setMotCle(String motCle) {
		this.motCle = motCle;
	}
 
	private String nomCat;
	private String description;
	private Vector lesCat=new Vector();
	public long getIdCat() {
		return idCat;
	}
	public void setIdCat(long idCat) {
		this.idCat = idCat;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Vector getLesCat() {
		return lesCat;
	}
	public void setLesCat(Vector lesCat) {
		this.lesCat = lesCat;
	}
	public String getNomCat() {
		return nomCat;
	}
	public void setNomCat(String nomCat) {
		this.nomCat = nomCat;
	}
 
}
Contrôleur ( Servlet : CatalogueServlet.java)

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
 import javax.servlet.http.*;
import javax.servlet.*;
 
import mod.catal.Catalogue;
import java.io.*;
import pres.beans.*;
public class CatalogueServlet extends HttpServlet {
	public void doPost(HttpServletRequest request,
			HttpServletResponse response)throws ServletException,IOException{
//Créer le bean qui va stocker les données de la requête
		CategorieForm cf=new CategorieForm();
//Faire appel au modèle pour ajouter une nouvelle catégorie
		Catalogue catal=new Catalogue();
		if(request.getParameter("addCat")!=null){
// Socker les données de la reqête dans le bean créé
			cf.setNomCat(request.getParameter("nomCat"));
			cf.setDescription(request.getParameter("description"));
//Sauvegarder l'objet dans la base de données
			catal.addCategorie(cf.getNomCat(),cf.getDescription());
//Récupérer toutes les catégries et les stocker dans le bean
			cf.setLesCat(catal.selectCatAll());
		}
		else if(request.getParameter("idCat")!=null){
// Socker les données de la reqête dans le bean créé
			cf.setIdCat(Long.parseLong(request.getParameter("idCat")));
 
//Supprimer l'objet de la base de données
			catal.DelCategorie(cf.getIdCat());
			cf.setLesCat(catal.selectCatAll());
		}
		else if(request.getParameter("chercheCat")!=null){
// Socker les données de la reqête dans le bean créé
			cf.setMotCle(request.getParameter("motCle"));
//Récupérer les catégries par mot clé et les stocker dans le bean
			cf.setLesCat(catal.selectCatByKeyword(cf.getMotCle()));
		}
		else{
//Récupérer toutes les catégries et les stocker dans le bean
			cf.setLesCat(catal.selectCatAll());
		}
 
/* avant de donner la main la page JSP pour afficher
 * enregitrer le bean dans la requête ou la session courante
 */
			HttpSession session=request.getSession();
			session.setAttribute("catForm",cf);
//Faire une redirection vers la vue JSP pour afficher.
			response.sendRedirect("Categories.jsp");		
		}
	public void doGet(HttpServletRequest request,
			HttpServletResponse response)throws ServletException,IOException{
			doPost(request,response);
	}
}
Descripteur de déploiement de la servlet : 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
 <?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
    <web-app>
        <servlet>
            <servlet-name>  catalServlet   </servlet-name>
            <servlet-class> CatalogueServlet </servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>catalServlet</servlet-name>
            <url-pattern>/catalogue</url-pattern>
        </servlet-mapping>
 
    </web-app>
La vue ( page JSP: Categories.jsp)

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
  <%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="mod.catal.*" %>
<%@ page import="pres.beans.*" %>
<html>
<head>
<title>Catégories</title>
</head>
<body bgcolor="#FFFFFF">
  <%  CategorieForm cf=(CategorieForm)session.getAttribute("catForm");%>
<form method='post' action='catalogue'>
 <table border='1' align='center'>
   <tr>
    <td>Chercher une catégorie:<input type='text' name='motCle' value='<%=cf.getMotCle()%>'></td>
    <td><input type='submit' name='chercheCat' value='Chercher'></td>
   </tr>
 </table>
</form>
<form method='post' action='catalogue'>
  <table border='1' align='center'>
    <tr>
      <td>Id Catégorie<td>Nom Catégorie</td><td>Description</td>
    </tr>
    <tr>
     <td></td>
     <td><input type='text' name='nomCat'></td>
     <td><input type='text' name='description' size='40'></td>
     <td><input type='submit' name='addCat' value='Ajouter'></td>
    </tr>
     <% if(cf!=null){
      Iterator lesCat=cf.getLesCat().iterator();
      while (lesCat.hasNext()){
       Categorie cat=(Categorie)lesCat.next();%>
     <tr>
      <td><%=cat.getIdCat()%></td>
      <td><%=cat.getTitre()%></td>
      <td><%=cat.getDescription()%></td>
      <td><a href='catalogue?idCat=<%=cat.getIdCat()%>'>Supprimer</a>
     </tr>
<%
      }
      }
    %>
  </table>
</form>
 
</body>
</html>
Mais quand j'essaye d'executer ce projet voici ce qui me donne comme résultat:

type Rapport d'exception

message

description Le serveur a rencontré une erreur interne () qui l'a empêché de satisfaire la requête.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


cause mère

java.lang.NullPointerException
org.apache.jsp.Categories_jsp._jspService(Categories_jsp.java:72)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/6.0.33.
Pourriez-vous me dire ou est le problème?

Et merci d'avance.