Browse Source

centralized server

LMAGallois 3 years ago
parent
commit
31d1638917

+ 50
- 0
Application/Clavardage/src/com/edu4java/servlets/FirstServlet.java View File

@@ -0,0 +1,50 @@
1
+package com.edu4java.servlets;
2
+
3
+ 
4
+
5
+import java.io.IOException;
6
+
7
+
8
+import java.io.PrintWriter;
9
+
10
+import java.util.Date;
11
+
12
+import javax.servlet.ServletException;
13
+
14
+import javax.servlet.http.HttpServlet;
15
+
16
+import javax.servlet.http.HttpServletRequest;
17
+
18
+import javax.servlet.http.HttpServletResponse;
19
+
20
+
21
+ 
22
+
23
+public class FirstServlet extends HttpServlet{
24
+
25
+ 
26
+
27
+ @Override
28
+
29
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
30
+
31
+                  throws ServletException, IOException {
32
+
33
+           
34
+
35
+            PrintWriter out = resp.getWriter();
36
+
37
+            out.println("<html>");
38
+
39
+            out.println("<body>");
40
+
41
+            out.println("hoy es " + new Date());
42
+
43
+            out.println("</body>");
44
+
45
+            out.println("</html>");
46
+
47
+ }
48
+
49
+
50
+}

+ 29
- 0
Application/Clavardage/src/com/sdzee/beans/Utilisateur.java View File

@@ -0,0 +1,29 @@
1
+package com.sdzee.beans;
2
+
3
+public class Utilisateur {
4
+
5
+    private String email;
6
+    private String motDePasse;
7
+    private String nom;
8
+
9
+    public void setEmail(String email) {
10
+	this.email = email;
11
+    }
12
+    public String getEmail() {
13
+	return email;
14
+    }
15
+
16
+    public void setMotDePasse(String motDePasse) {
17
+	this.motDePasse = motDePasse;
18
+    }
19
+    public String getMotDePasse() {
20
+	return motDePasse;
21
+    }
22
+
23
+    public void setNom(String nom) {
24
+	this.nom = nom;
25
+    }
26
+    public String getNom() {
27
+	return nom;
28
+    }
29
+}

+ 117
- 0
Application/Clavardage/src/com/sdzee/forms/InscriptionForm.java View File

@@ -0,0 +1,117 @@
1
+package com.sdzee.forms;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+import javax.servlet.http.HttpServletRequest;
7
+
8
+import com.sdzee.beans.Utilisateur;
9
+
10
+public final class InscriptionForm {
11
+    private static final String CHAMP_EMAIL  = "email";
12
+    private static final String CHAMP_PASS   = "motdepasse";
13
+    private static final String CHAMP_CONF   = "confirmation";
14
+    private static final String CHAMP_NOM    = "nom";
15
+   
16
+
17
+    private String              resultat;
18
+    private Map<String, String> erreurs      = new HashMap<String, String>();
19
+
20
+    public String getResultat() {
21
+        return resultat;
22
+    }
23
+
24
+    public Map<String, String> getErreurs() {
25
+        return erreurs;
26
+    }
27
+
28
+ 
29
+
30
+    public Utilisateur inscrireUtilisateur( HttpServletRequest request ) {
31
+        String email = getValeurChamp( request, CHAMP_EMAIL );
32
+        String motDePasse = getValeurChamp( request, CHAMP_PASS );
33
+        String confirmation = getValeurChamp( request, CHAMP_CONF );
34
+        String nom = getValeurChamp( request, CHAMP_NOM );
35
+
36
+        Utilisateur utilisateur = new Utilisateur();
37
+
38
+        try {
39
+            validationEmail( email );
40
+        } catch ( Exception e ) {
41
+            setErreur( CHAMP_EMAIL, e.getMessage() );
42
+        }
43
+        utilisateur.setEmail( email );
44
+
45
+        try {
46
+            validationMotsDePasse( motDePasse, confirmation );
47
+        } catch ( Exception e ) {
48
+            setErreur( CHAMP_PASS, e.getMessage() );
49
+            setErreur( CHAMP_CONF, null );
50
+        }
51
+        utilisateur.setMotDePasse( motDePasse );
52
+
53
+        try {
54
+            validationNom( nom );
55
+        } catch ( Exception e ) {
56
+            setErreur( CHAMP_NOM, e.getMessage() );
57
+        }
58
+        utilisateur.setNom( nom );
59
+
60
+        if ( erreurs.isEmpty() ) {
61
+            resultat = "Succès de l'inscription.";
62
+        } else {
63
+            resultat = "Échec de l'inscription.";
64
+        }
65
+
66
+        return utilisateur;
67
+    }
68
+
69
+
70
+    private void validationEmail( String email ) throws Exception {
71
+        if ( email != null ) {
72
+            if ( !email.matches( "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) ) {
73
+                throw new Exception( "Merci de saisir une adresse mail valide." );
74
+            }
75
+        } else {
76
+            throw new Exception( "Merci de saisir une adresse mail." );
77
+        }
78
+    }
79
+
80
+    private void validationMotsDePasse( String motDePasse, String confirmation ) throws Exception {
81
+        if ( motDePasse != null && confirmation != null ) {
82
+            if ( !motDePasse.equals( confirmation ) ) {
83
+                throw new Exception( "Les mots de passe entrés sont différents, merci de les saisir à nouveau." );
84
+            } else if ( motDePasse.length() < 3 ) {
85
+                throw new Exception( "Les mots de passe doivent contenir au moins 3 caractères." );
86
+            }
87
+        } else {
88
+            throw new Exception( "Merci de saisir et confirmer votre mot de passe." );
89
+        }
90
+    }
91
+
92
+    private void validationNom( String nom ) throws Exception {
93
+        if ( nom != null && nom.length() < 3 ) {
94
+            throw new Exception( "Le nom d'utilisateur doit contenir au moins 3 caractères." );
95
+        }
96
+    }
97
+
98
+    /*
99
+     * Ajoute un message correspondant au champ spécifié à la map des erreurs.
100
+     */
101
+    private void setErreur( String champ, String message ) {
102
+        erreurs.put( champ, message );
103
+    }
104
+
105
+    /*
106
+     * Méthode utilitaire qui retourne null si un champ est vide, et son contenu
107
+     * sinon.
108
+     */
109
+    private static String getValeurChamp( HttpServletRequest request, String nomChamp ) {
110
+        String valeur = request.getParameter( nomChamp );
111
+        if ( valeur == null || valeur.trim().length() == 0 ) {
112
+            return null;
113
+        } else {
114
+            return valeur.trim();
115
+        }
116
+    }
117
+}

+ 36
- 0
Application/Clavardage/src/com/sdzee/servlets/Inscription.java View File

@@ -0,0 +1,36 @@
1
+package com.sdzee.servlets;
2
+
3
+import java.io.IOException;
4
+
5
+import javax.servlet.ServletException;
6
+import javax.servlet.http.HttpServlet;
7
+import javax.servlet.http.HttpServletRequest;
8
+import javax.servlet.http.HttpServletResponse;
9
+
10
+import com.sdzee.beans.Utilisateur;
11
+import com.sdzee.forms.InscriptionForm;
12
+
13
+public class Inscription extends HttpServlet {
14
+    public static final String ATT_USER = "utilisateur";
15
+    public static final String ATT_FORM = "form";
16
+    public static final String VUE = "/WEB-INF/inscription.jsp";
17
+		
18
+    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
19
+        /* Affichage de la page d'inscription */
20
+        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
21
+    }
22
+	
23
+    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
24
+        /* Préparation de l'objet formulaire */
25
+        InscriptionForm form = new InscriptionForm();
26
+		
27
+        /* Appel au traitement et à la validation de la requête, et récupération du bean en résultant */
28
+        Utilisateur utilisateur = form.inscrireUtilisateur( request );
29
+		
30
+        /* Stockage du formulaire et du bean dans l'objet request */
31
+        request.setAttribute( ATT_FORM, form );
32
+        request.setAttribute( ATT_USER, utilisateur );
33
+		
34
+        this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
35
+    }
36
+}

Loading…
Cancel
Save