suppression servlet
This commit is contained in:
parent
2d48a2b0d4
commit
fb7c74480c
4 changed files with 0 additions and 232 deletions
|
@ -1,50 +0,0 @@
|
||||||
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>");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,117 +0,0 @@
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
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