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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package 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 beans.Message;
import forms.ContactForm;
import utility.EmailUtility;
import static servlets.Contact.ATT_FORM;
import static servlets.Contact.ATT_MESSAGE;
import static servlets.Contact.ATT_MSG;
import static servlets.Contact.VUE_ECHEC;
import static servlets.Contact.VUE_SUCCES;
public class Contact extends HttpServlet {
public static final String ATT_MSG = "message";
public static final String ATT_FORM = "form";
public static final String ATT_MESSAGE = "msg";
public static final String VUE_SUCCES = "/envoireussi.jsp";
public static final String VUE_ECHEC = "/contact.jsp";
public void init() throws ServletException {
}
public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
/* À la réception d'une requête GET, simple affichage du formulaire */
this.getServletContext().getRequestDispatcher( VUE_SUCCES ).forward( request, response );
}
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
/* Préparation de l'objet formulaire */
ContactForm form = new ContactForm( );
/* Traitement de la requête et récupération du bean en résultant */
Message message = form.creerMessage( request );
/* Ajout du bean et de l'objet métier à l'objet requête */
request.setAttribute( ATT_MSG, message );
request.setAttribute( ATT_FORM, form );
String msgRetour = "";
Long id = message.getId();
String entrep = message.getEntrep();
String nom = message.getNom();
String prenom = message.getPrenom();
String email = message.getEmail();
String sujet = message.getSujet();
String msg = message.getMessage();
/* Si aucune erreur */
if ( form.getErreurs().isEmpty() ) {
StringBuilder builder = new StringBuilder("<html><body>");
builder.append("Nom:").append(nom).append("<br><br> Prénom:").append(prenom).append("<br><br>Entreprise: ").append(entrep).append(" <br><br>Message: ").append(msg);
builder.append("</body></html>");
String emailBody = builder.toString(); // le bouton est moche, suis d'accord :D
try {
EmailUtility.sendEmail( email, sujet, emailBody);
msgRetour = "<span id="+"resultat_vert"+">Message envoyé avec succès !</span>";
request.setAttribute(ATT_MESSAGE, msgRetour);
this.getServletContext().getRequestDispatcher(VUE_SUCCES).forward(request, response);
}
catch (Exception ex) {
System.out.println(ex);
msgRetour = "<div class=\"echec2\">Message non envoyé! Adresse Email non valide ou problème de connexion. Veuillez réessayer</div>" ;
request.setAttribute(ATT_MESSAGE, msgRetour);
this.getServletContext().getRequestDispatcher(VUE_ECHEC).forward(request, response);
}
finally {
}
} else {
/* Sinon, ré-affichage du formulaire de création avec les erreurs */
this.getServletContext().getRequestDispatcher( VUE_ECHEC ).forward( request, response );
}
}
} |
Partager