centralized server
This commit is contained in:
parent
21780d4c4c
commit
31d1638917
4 changed files with 232 additions and 0 deletions
|
@ -0,0 +1,50 @@
|
|||
package com.edu4java.servlets;
|
||||
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
||||
|
||||
|
||||
public class FirstServlet extends HttpServlet{
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
|
||||
throws ServletException, IOException {
|
||||
|
||||
|
||||
|
||||
PrintWriter out = resp.getWriter();
|
||||
|
||||
out.println("<html>");
|
||||
|
||||
out.println("<body>");
|
||||
|
||||
out.println("hoy es " + new Date());
|
||||
|
||||
out.println("</body>");
|
||||
|
||||
out.println("</html>");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
29
Application/Clavardage/src/com/sdzee/beans/Utilisateur.java
Normal file
29
Application/Clavardage/src/com/sdzee/beans/Utilisateur.java
Normal file
|
@ -0,0 +1,29 @@
|
|||
package com.sdzee.beans;
|
||||
|
||||
public class Utilisateur {
|
||||
|
||||
private String email;
|
||||
private String motDePasse;
|
||||
private String nom;
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setMotDePasse(String motDePasse) {
|
||||
this.motDePasse = motDePasse;
|
||||
}
|
||||
public String getMotDePasse() {
|
||||
return motDePasse;
|
||||
}
|
||||
|
||||
public void setNom(String nom) {
|
||||
this.nom = nom;
|
||||
}
|
||||
public String getNom() {
|
||||
return nom;
|
||||
}
|
||||
}
|
117
Application/Clavardage/src/com/sdzee/forms/InscriptionForm.java
Normal file
117
Application/Clavardage/src/com/sdzee/forms/InscriptionForm.java
Normal file
|
@ -0,0 +1,117 @@
|
|||
package com.sdzee.forms;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.sdzee.beans.Utilisateur;
|
||||
|
||||
public final class InscriptionForm {
|
||||
private static final String CHAMP_EMAIL = "email";
|
||||
private static final String CHAMP_PASS = "motdepasse";
|
||||
private static final String CHAMP_CONF = "confirmation";
|
||||
private static final String CHAMP_NOM = "nom";
|
||||
|
||||
|
||||
private String resultat;
|
||||
private Map<String, String> erreurs = new HashMap<String, String>();
|
||||
|
||||
public String getResultat() {
|
||||
return resultat;
|
||||
}
|
||||
|
||||
public Map<String, String> getErreurs() {
|
||||
return erreurs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Utilisateur inscrireUtilisateur( HttpServletRequest request ) {
|
||||
String email = getValeurChamp( request, CHAMP_EMAIL );
|
||||
String motDePasse = getValeurChamp( request, CHAMP_PASS );
|
||||
String confirmation = getValeurChamp( request, CHAMP_CONF );
|
||||
String nom = getValeurChamp( request, CHAMP_NOM );
|
||||
|
||||
Utilisateur utilisateur = new Utilisateur();
|
||||
|
||||
try {
|
||||
validationEmail( email );
|
||||
} catch ( Exception e ) {
|
||||
setErreur( CHAMP_EMAIL, e.getMessage() );
|
||||
}
|
||||
utilisateur.setEmail( email );
|
||||
|
||||
try {
|
||||
validationMotsDePasse( motDePasse, confirmation );
|
||||
} catch ( Exception e ) {
|
||||
setErreur( CHAMP_PASS, e.getMessage() );
|
||||
setErreur( CHAMP_CONF, null );
|
||||
}
|
||||
utilisateur.setMotDePasse( motDePasse );
|
||||
|
||||
try {
|
||||
validationNom( nom );
|
||||
} catch ( Exception e ) {
|
||||
setErreur( CHAMP_NOM, e.getMessage() );
|
||||
}
|
||||
utilisateur.setNom( nom );
|
||||
|
||||
if ( erreurs.isEmpty() ) {
|
||||
resultat = "Succès de l'inscription.";
|
||||
} else {
|
||||
resultat = "Échec de l'inscription.";
|
||||
}
|
||||
|
||||
return utilisateur;
|
||||
}
|
||||
|
||||
|
||||
private void validationEmail( String email ) throws Exception {
|
||||
if ( email != null ) {
|
||||
if ( !email.matches( "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) ) {
|
||||
throw new Exception( "Merci de saisir une adresse mail valide." );
|
||||
}
|
||||
} else {
|
||||
throw new Exception( "Merci de saisir une adresse mail." );
|
||||
}
|
||||
}
|
||||
|
||||
private void validationMotsDePasse( String motDePasse, String confirmation ) throws Exception {
|
||||
if ( motDePasse != null && confirmation != null ) {
|
||||
if ( !motDePasse.equals( confirmation ) ) {
|
||||
throw new Exception( "Les mots de passe entrés sont différents, merci de les saisir à nouveau." );
|
||||
} else if ( motDePasse.length() < 3 ) {
|
||||
throw new Exception( "Les mots de passe doivent contenir au moins 3 caractères." );
|
||||
}
|
||||
} else {
|
||||
throw new Exception( "Merci de saisir et confirmer votre mot de passe." );
|
||||
}
|
||||
}
|
||||
|
||||
private void validationNom( String nom ) throws Exception {
|
||||
if ( nom != null && nom.length() < 3 ) {
|
||||
throw new Exception( "Le nom d'utilisateur doit contenir au moins 3 caractères." );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Ajoute un message correspondant au champ spécifié à la map des erreurs.
|
||||
*/
|
||||
private void setErreur( String champ, String message ) {
|
||||
erreurs.put( champ, message );
|
||||
}
|
||||
|
||||
/*
|
||||
* Méthode utilitaire qui retourne null si un champ est vide, et son contenu
|
||||
* sinon.
|
||||
*/
|
||||
private static String getValeurChamp( HttpServletRequest request, String nomChamp ) {
|
||||
String valeur = request.getParameter( nomChamp );
|
||||
if ( valeur == null || valeur.trim().length() == 0 ) {
|
||||
return null;
|
||||
} else {
|
||||
return valeur.trim();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.sdzee.servlets;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.sdzee.beans.Utilisateur;
|
||||
import com.sdzee.forms.InscriptionForm;
|
||||
|
||||
public class Inscription extends HttpServlet {
|
||||
public static final String ATT_USER = "utilisateur";
|
||||
public static final String ATT_FORM = "form";
|
||||
public static final String VUE = "/WEB-INF/inscription.jsp";
|
||||
|
||||
public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
|
||||
/* Affichage de la page d'inscription */
|
||||
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
|
||||
}
|
||||
|
||||
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
|
||||
/* Préparation de l'objet formulaire */
|
||||
InscriptionForm form = new InscriptionForm();
|
||||
|
||||
/* Appel au traitement et à la validation de la requête, et récupération du bean en résultant */
|
||||
Utilisateur utilisateur = form.inscrireUtilisateur( request );
|
||||
|
||||
/* Stockage du formulaire et du bean dans l'objet request */
|
||||
request.setAttribute( ATT_FORM, form );
|
||||
request.setAttribute( ATT_USER, utilisateur );
|
||||
|
||||
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue