Correction Java EE 1
Correction Java EE
Exercices - Architecture Entreprise
Exercice 1 : Architecture et Patrons de Conception (5 pts)
1. Architecture J2EE Standard et rôle de chaque couche (1,5 pt)
L’architecture J2EE suit un modèle en couches :
— Couche Présentation : Interface utilisateur, JSP, servlets
— Couche Métier : Logique applicative, EJB, services métier
— Couche Données : Accès aux données, bases de données
— Couche Client : Navigateurs web, applications clientes
2. Patrons de Conception en JavaEE (0,5 pt)
— MVC : Séparation des préoccupations
— DAO : Encapsulation de l’accès aux données
— Factory : Création d’objets
— Singleton : Instance unique
— Observer : Notification d’événements
3. Modèle d’architecture JavaEE (1,5 pt)
Séparation des préoccupations :
— Modèle : Entités métier, logique de données
— Vue : Interface utilisateur (JSP, HTML)
— Contrôleur : Gestion des requêtes (Servlets)
Rôle des composants :
— Servlets : Traitement des requêtes HTTP
— JSP : Génération dynamique de contenu
— EJB : Logique métier et accès aux données
4. Différences Java SE, Java EE et JavaScript (1,5 pt)
Aspect Java SE Java EE JavaScript
Usage Applications desktop Applications entre- Applications web
prise client
Environnement JVM locale Serveur d’applications Navigateur web
Composants Classes, interfaces Servlets, JSP, EJB Fonctions, objets
Exemples Calculatrice Sites e-commerce Validation formulaires
Exercices - Architecture Entreprise
Correction Java EE 2
Exercice 2 : Sessions et Cookies (4 pts)
1. Différence entre cookies et sessions (1 pt)
Aspect Cookies Sessions
Stockage Côté client Côté serveur
Persistance Persistants Temporaires
Capacité Limitée (4KB) Illimitée
Sécurité Visibles utilisateur Sécurisées
2. Rôle de la servlet (0,5 pt)
La servlet gère le cycle de vie des requêtes HTTP, traite les paramètres et génère les réponses.
3. Obtenir la session courante (0,5 pt)
1 HttpSession session = request . getSession () ;
4. Créer et stocker un attribut (0,5 pt)
1 HttpSession session = request . getSession () ;
2 session . setAttribute ( " nomAttribut " , valeur ) ;
5. Stockage des cookies (0,5 pt)
1 Cookie cookie = new Cookie ( " nomCookie " , " valeur " ) ;
2 cookie . setMaxAge (3600) ;
3 response . addCookie ( cookie ) ;
6. Risques cookies pour sessions (1 pt)
— Sécurité : Interception, falsification
— Confidentialité : Données exposées
— Fiabilité : Cookies désactivés
— Limitation : Taille restreinte
— Performance : Transfert à chaque requête
Exercices - Architecture Entreprise
Correction Java EE 3
Problème : Application Gestion Avis de Stage (11 pts)
1. Fichier web.xml avec mapping 3 servlets (1,5 pt)
1 <? xml version = " 1.0 " encoding = " UTF -8 " ? >
2 <web - app xmlns = " http: // java . sun . com / xml / ns / javaee "
3 version = " 3.0 " >
4
5 < servlet >
6 < servlet - name > AjoutAvis </ servlet - name >
7 < servlet - class > servlets . AjoutAvis </ servlet - class >
8 </ servlet >
9
10 < servlet >
11 < servlet - name > SuppressionAvis </ servlet - name >
12 < servlet - class > servlets . SuppressionAvis </ servlet - class >
13 </ servlet >
14
15 < servlet >
16 < servlet - name > ListeAvis </ servlet - name >
17 < servlet - class > servlets . ListeAvis </ servlet - class >
18 </ servlet >
19
20 < servlet - mapping >
21 < servlet - name > AjoutAvis </ servlet - name >
22 <url - pattern >/ ajouterAvis </ url - pattern >
23 </ servlet - mapping >
24
25 < servlet - mapping >
26 < servlet - name > SuppressionAvis </ servlet - name >
27 <url - pattern >/ supprimerAvis </ url - pattern >
28 </ servlet - mapping >
29
30 < servlet - mapping >
31 < servlet - name > ListeAvis </ servlet - name >
32 <url - pattern >/ listerAvis </ url - pattern >
33 </ servlet - mapping >
34
35 </ web - app >
2. Bean AvisDeStage.java (2 pts)
1 package beans ;
2
3 public class AvisDeStage implements java . io . Serializable {
4 private int id ;
5 private String nomEtudiant ;
6 private String filiere ;
7 private String nomEntreprise ;
8 private int satisfaction ;
9
10 public AvisDeStage () {}
11
12 public AvisDeStage ( int id , String nomEtudiant , String filiere ,
13 String nomEntreprise , int satisfaction ) {
14 this . id = id ;
15 this . nomEtudiant = nomEtudiant ;
Exercices - Architecture Entreprise
Correction Java EE 4
16 this . filiere = filiere ;
17 this . nomEntreprise = nomEntreprise ;
18 this . satisfaction = satisfaction ;
19 }
20
21 // Getters
22 public int getId () { return id ; }
23 public String getNomEtudiant () { return nomEtudiant ; }
24 public String getFiliere () { return filiere ; }
25 public String getNomEntreprise () { return nomEntreprise ; }
26 public int getSatisfaction () { return satisfaction ; }
27
28 // Setters
29 public void setId ( int id ) { this . id = id ; }
30 public void setNomEtudiant ( String nomEtudiant ) {
31 this . nomEtudiant = nomEtudiant ;
32 }
33 public void setFiliere ( String filiere ) {
34 this . filiere = filiere ;
35 }
36 public void setNomEntreprise ( String nomEntreprise ) {
37 this . nomEntreprise = nomEntreprise ;
38 }
39 public void setSatisfaction ( int satisfaction ) {
40 this . satisfaction = satisfaction ;
41 }
42 }
3. Page JSP soumettreAvis.jsp (2 pts)
1 <% @ page contentType = " text / html ; charset = UTF -8 " language = " java " % >
2 <! DOCTYPE html >
3 < html >
4 < head >
5 < meta charset = " UTF -8 " >
6 < title > Soumettre un Avis de Stage </ title >
7 < style >
8 body { font - family : Arial , sans - serif ; margin : 40 px ; }
9 . form - container { max - width : 600 px ; margin : 0 auto ; }
10 . form - group { margin - bottom : 15 px ; }
11 label { display : block ; margin - bottom : 5 px ; font - weight : bold ; }
12 input , select { width : 100%; padding : 8 px ;
13 border : 1 px solid # ccc ; border - radius : 4 px ; }
14 button { background - color : #007 bff ; color : white ;
15 padding : 10 px 20 px ; border : none ;
16 border - radius : 4 px ; cursor : pointer ; }
17 </ style >
18 </ head >
19 < body >
20 < div class = " form - container " >
21 < h2 > Soumettre un Avis de Stage </ h2 >
22 < form action = " ajouterAvis " method = " post " >
23 < div class = " form - group " >
24 < label for = " nomEtudiant " > Nom de l ’ tudiant : </ label >
25 < input type = " text " id = " nomEtudiant "
26 name = " nomEtudiant " required >
27 </ div >
28
29 < div class = " form - group " >
Exercices - Architecture Entreprise
Correction Java EE 5
30 < label for = " filiere " > F i l i r e : </ label >
31 < select id = " filiere " name = " filiere " required >
32 < option value = " " > S l e c t i o n n e z une f i l i r e </ option >
33 < option value = " Informatique " > Informatique </ option >
34 < option value = " Gestion " > Gestion </ option >
35 < option value = " Marketing " > Marketing </ option >
36 < option value = " Finance " > Finance </ option >
37 </ select >
38 </ div >
39
40 < div class = " form - group " >
41 < label for = " nomEntreprise " > Nom de l ’ entreprise : </ label >
42 < input type = " text " id = " nomEntreprise "
43 name = " nomEntreprise " required >
44 </ div >
45
46 < div class = " form - group " >
47 < label for = " satisfaction " > Satisfaction (1 -5) : </ label >
48 < select id = " satisfaction " name = " satisfaction " required >
49 < option value = " " > Choisissez une note </ option >
50 < option value = " 1 " >1 - T r s insatisfait </ option >
51 < option value = " 2 " >2 - Insatisfait </ option >
52 < option value = " 3 " >3 - Neutre </ option >
53 < option value = " 4 " >4 - Satisfait </ option >
54 < option value = " 5 " >5 - T r s satisfait </ option >
55 </ select >
56 </ div >
57
58 < button type = " submit " > Soumettre l ’ avis </ button >
59 < button type = " reset " > R i n i t i a l i s e r </ button >
60 </ form >
61 </ div >
62 </ body >
63 </ html >
4. Servlet AjoutAvis.java (2 pts)
1 package servlets ;
2
3 import beans . AvisDeStage ;
4 import dao . AvisDAO ;
5 import javax . servlet . ServletException ;
6 import javax . servlet . http . HttpServlet ;
7 import javax . servlet . http . H tt pSe rv le tRe qu es t ;
8 import javax . servlet . http . Ht t pS e rv l e tR e sp o n se ;
9 import java . io . IOException ;
10
11 public class AjoutAvis extends HttpServlet {
12
13 private AvisDAO avisDAO ;
14
15 @Override
16 public void init () throws ServletException {
17 avisDAO = new AvisDAO () ;
18 }
19
20 @Override
21 protected void doGet ( Ht tp Ser vl et Req ue st request ,
22 H tt p S er v le t R es p on s e response )
Exercices - Architecture Entreprise
Correction Java EE 6
23 throws ServletException , IOException {
24 request . g e t R e q ue s t D i s p a t c h e r ( " / soumettreAvis . jsp " )
25 . forward ( request , response ) ;
26 }
27
28 @Override
29 protected void doPost ( Ht tp Ser vl et Req ue st request ,
30 H tt p S er v le t R es p on s e response )
31 throws ServletException , IOException {
32
33 try {
34 String nomEtudiant = request . getParameter ( " nomEtudiant " ) ;
35 String filiere = request . getParameter ( " filiere " ) ;
36 String nomEntreprise = request . getParameter ( " nomEntreprise " ) ;
37 int satisfaction = Integer . parseInt (
38 request . getParameter ( " satisfaction " ) ) ;
39
40 AvisDeStage avis = new AvisDeStage () ;
41 avis . setNomEtudiant ( nomEtudiant ) ;
42 avis . setFiliere ( filiere ) ;
43 avis . setNomEntreprise ( nomEntreprise ) ;
44 avis . setSatisfaction ( satisfaction ) ;
45
46 boolean success = avisDAO . ajouterAvis ( avis ) ;
47
48 if ( success ) {
49 response . sendRedirect ( " confirmation . jsp " ) ;
50 } else {
51 response . sendRedirect ( " erreur . jsp " ) ;
52 }
53
54 } catch ( Exception e ) {
55 e . printStackTrace () ;
56 response . sendRedirect ( " erreur . jsp " ) ;
57 }
58 }
59 }
5. Classe AvisDAO.java (1,5 pt)
1 package dao ;
2
3 import beans . AvisDeStage ;
4 import java . sql .*;
5 import java . util . ArrayList ;
6 import java . util . List ;
7
8 public class AvisDAO {
9
10 private static final String URL =
11 " jdbc : mysql :// localhost :3306/ gestion_stages " ;
12 private static final String USERNAME = " root " ;
13 private static final String PASSWORD = " password " ;
14
15 private Connection getConnection () throws SQLException {
16 return DriverManager . getConnection ( URL , USERNAME , PASSWORD ) ;
17 }
18
19 public boolean ajouterAvis ( AvisDeStage avis ) {
Exercices - Architecture Entreprise
Correction Java EE 7
20 String sql = " INSERT INTO avis_stage ( nom_etudiant , filiere , " +
21 " nom_entreprise , satisfaction ) VALUES (? , ? , ? , ?) " ;
22
23 try ( Connection conn = getConnection () ;
24 Prepa redState ment stmt = conn . prepareStatement ( sql ) ) {
25
26 stmt . setString (1 , avis . getNomEtudiant () ) ;
27 stmt . setString (2 , avis . getFiliere () ) ;
28 stmt . setString (3 , avis . getNomEntreprise () ) ;
29 stmt . setInt (4 , avis . getSatisfaction () ) ;
30
31 return stmt . executeUpdate () > 0;
32
33 } catch ( SQLException e ) {
34 e . printStackTrace () ;
35 return false ;
36 }
37 }
38
39 public boolean supprimerAvis ( int id ) {
40 String sql = " DELETE FROM avis_stage WHERE id = ? " ;
41
42 try ( Connection conn = getConnection () ;
43 Prepa redState ment stmt = conn . prepareStatement ( sql ) ) {
44
45 stmt . setInt (1 , id ) ;
46 return stmt . executeUpdate () > 0;
47
48 } catch ( SQLException e ) {
49 e . printStackTrace () ;
50 return false ;
51 }
52 }
53
54 public List < AvisDeStage > lister TousLesA vis () {
55 List < AvisDeStage > avis = new ArrayList < >() ;
56 String sql = " SELECT * FROM avis_stage ORDER BY id DESC " ;
57
58 try ( Connection conn = getConnection () ;
59 Statement stmt = conn . createStatement () ;
60 ResultSet rs = stmt . executeQuery ( sql ) ) {
61
62 while ( rs . next () ) {
63 AvisDeStage avisStage = new AvisDeStage () ;
64 avisStage . setId ( rs . getInt ( " id " ) ) ;
65 avisStage . setNomEtudiant ( rs . getString ( " nom_etudiant " ) ) ;
66 avisStage . setFiliere ( rs . getString ( " filiere " ) ) ;
67 avisStage . setNomEntreprise ( rs . getString ( " nom_entreprise " ) ) ;
68 avisStage . setSatisfaction ( rs . getInt ( " satisfaction " ) ) ;
69
70 avis . add ( avisStage ) ;
71 }
72
73 } catch ( SQLException e ) {
74 e . printStackTrace () ;
75 }
76
77 return avis ;
78 }
79 }
Exercices - Architecture Entreprise
Correction Java EE 8
6. Page JSP listeAvis.jsp avec JSTL (1 pt)
n
1 <% @ page contentType = " text / html ; charset = UTF -8 " language = " java " % >
2 <% @ taglib uri = " http :// java . sun . com / jsp / jstl / core " prefix = " c " % >
3 <! DOCTYPE html >
4 < html >
5 < head >
6 < meta charset = " UTF -8 " >
7 < title > Liste des Avis de Stage </ title >
8 < style >
9 body { font - family : Arial , sans - serif ; margin : 20 px ; }
10 table { width : 100%; border - collapse : collapse ; margin - top : 20 px ; }
11 th , td { border : 1 px solid # ddd ; padding : 12 px ; text - align : left ; }
12 th { background - color : # f2f2f2 ; font - weight : bold ; }
13 tr : nth - child ( even ) { background - color : # f9f9f9 ; }
14 . btn { padding : 5 px 10 px ; margin : 2 px ; text - decoration : none ;
15 border - radius : 3 px ; }
16 . btn - danger { background - color : # dc3545 ; color : white ; }
17 </ style >
18 </ head >
19 < body >
20 < h1 > Liste des Avis de Stage </ h1 >
21
22 <p > <a href = " ajouterAvis " class = " btn "
23 style = " background - color : #007 bff ; color : white ; " >
24 Ajouter un nouvel avis </ a > </ p >
25
26 <c : choose >
27 <c : when test = " $ { empty listeAvis } " >
28 <p > Aucun avis de stage e n r e g i s t r . </ p >
29 </ c : when >
30 <c : otherwise >
31 < table >
32 < thead >
33 < tr >
34 < th > ID </ th >
35 < th > Nom tudiant </ th >
36 < th > F i l i r e </ th >
37 < th > Entreprise </ th >
38 < th > Satisfaction </ th >
39 < th > Actions </ th >
40 </ tr >
41 </ thead >
42 < tbody >
43 <c : forEach var = " avis " items = " $ { listeAvis } " >
44 < tr >
45 < td >$ { avis . id } </ td >
46 < td >$ { avis . nomEtudiant } </ td >
47 < td >$ { avis . filiere } </ td >
48 < td >$ { avis . nomEntreprise } </ td >
49 < td >$ { avis . satisfaction }/5 </ td >
50 < td >
51 <a href = " supprimerAvis ? id = $ { avis . id } "
52 class = " btn btn - danger " > Supprimer </ a >
53 </ td >
54 </ tr >
55 </ c : forEach >
56 </ tbody >
57 </ table >
Exercices - Architecture Entreprise
Correction Java EE 9
58 </ c : otherwise >
59 </ c : choose >
60 </ body >
61 </ html >
7. Servlet SuppressionAvis.java (1 pt)
1 package servlets ;
2
3 import dao . AvisDAO ;
4 import javax . servlet . ServletException ;
5 import javax . servlet . http . HttpServlet ;
6 import javax . servlet . http . H tt pSe rv le tRe qu es t ;
7 import javax . servlet . http . Ht t pS e rv l e tR e sp o n se ;
8 import java . io . IOException ;
9
10 public class SuppressionAvis extends HttpServlet {
11
12 private AvisDAO avisDAO ;
13
14 @Override
15 public void init () throws ServletException {
16 avisDAO = new AvisDAO () ;
17 }
18
19 @Override
20 protected void doGet ( Ht tp Ser vl et Req ue st request ,
21 H tt p S er v le t R es p on s e response )
22 throws ServletException , IOException {
23
24 try {
25 String idParam = request . getParameter ( " id " ) ;
26
27 if ( idParam != null && ! idParam . trim () . isEmpty () ) {
28 int id = Integer . parseInt ( idParam ) ;
29 boolean success = avisDAO . supprimerAvis ( id ) ;
30
31 if ( success ) {
32 response . sendRedirect ( " listerAvis " ) ;
33 } else {
34 response . sendRedirect ( " erreur . jsp " ) ;
35 }
36 } else {
37 response . sendRedirect ( " erreur . jsp " ) ;
38 }
39
40 } catch ( N u m b e r F o r m a t E x c e p t i o n e ) {
41 response . sendRedirect ( " erreur . jsp " ) ;
42 } catch ( Exception e ) {
43 e . printStackTrace () ;
44 response . sendRedirect ( " erreur . jsp " ) ;
45 }
46 }
47 }
Exercices - Architecture Entreprise