|
Par
cysboy
Mise à jour : 18/02/2011
10 160 visites depuis 7 jours,
dont 220 sur ce chapitre
classé 25/786
|








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 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.util.HashMap" %> <% //On récupère la liste des cookies Cookie[] cookies = request.getCookies(); HashMap map = new HashMap(); //S'il y a au moins un cookie, on stocke les clés-valeurs dans une Map if(cookies != null){ for(Cookie cookie : cookies){ map.put(cookie.getName(), cookie.getValue()); } } //Ainsi, si des données existent, on pourra préremplir les champs de notre formulaire %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Connexion à l'application</title> </head> <body> <form name="session" method="POST" action="connexion.do" /> <table style=" margin:auto; margin-top:50px; border-collapse:collapse; width:350px; text-align:right"> <thead> <tr> <td colspan=2 style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center" >Connexion</td> </tr> </thead> <tbody style=" background-color:#AAB0DF; border-radius-bottom-left:5px; border-radius-bottom-right:5px;" > <tr> <td>Login : </td> <td style="text-align:left;padding-left:20px"> <input type="text" name="login" value="<%=((map.get("login") != null) ? map.get("login") : "") %>"/> </td> </tr> <tr> <td>Mot de passe : </td> <td style="text-align:left;padding-left:20px" > <input type="password" name="password" value="<%=((map.get("login") != null) ? map.get("password") : "") %>"/> </td> </tr> <tr> <td>Connexion automatique</td> <td style="text-align:left;padding-left:20px" > <input type="checkbox" name="auto_connect" value="Y" <%=(((map.get("login") != null) && (map.get("login") != null)) ? "checked" : "") %>/> </td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" name="valider" /></td> </tr> </tbody> </table> </form> </body> </html> |
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 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Page 1</title> </head> <body> <table style=" margin:auto; margin-top:50px; border-collapse:collapse; width:350px; text-align:right"> <thead> <tr> <td style=" background-color:white; color:#404AA4; font-weight:bold; text-align:center" >Page 1 </td> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black" > <a href="http://localhost:8080/TP2 - CONNEXION/pages/page2.jsp" style="color:white;text-decoration:none">Page 2</a> </td> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black" > <a href="http://localhost:8080/TP2 - CONNEXION/pages/page3.jsp" style="color:white;text-decoration:none">Page 3</a> </td> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black" > <a href="http://localhost:8080/TP2 - CONNEXION/disconnect.do" style="color:white;text-decoration:none">Se déconnecter</a> </td> </tr> </thead> <tbody style=" background-color:#AAB0DF; height:150px; text-align:center; border:1px solid black " > <tr> <td colspan=4>Vous êtes sur la page 1.</td> </tr> </tbody> </table> </body> </html> |
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 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Page 2</title> </head> <body> <table style=" margin:auto; margin-top:50px; border-collapse:collapse; width:350px; text-align:right"> <thead> <tr> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black" > <a href="http://localhost:8080/TP2 - CONNEXION/pages/page1.jsp" style="color:white;text-decoration:none">Page 1</a> </td> <td style=" background-color:white; color:#404AA4; font-weight:bold; text-align:center" > Page 2 </td> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black" > <a href="http://localhost:8080/TP2 - CONNEXION/pages/page3.jsp" style="color:white;text-decoration:none">Page 3</a> </td> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black" > <a href="http://localhost:8080/TP2 - CONNEXION/disconnect.do" style="color:white;text-decoration:none">Se déconnecter</a> </td> </tr> </thead> <tbody style=" background-color:#AAB0DF; height:150px; text-align:center; border:1px solid black " > <tr> <td colspan=4>Vous êtes sur la page 2.</td> </tr> </tbody> </table> </body> </html> |
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 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Page 3</title> </head> <body> <table style=" margin:auto; margin-top:50px; border-collapse:collapse; width:350px; text-align:right"> <thead> <tr> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black" > <a href="http://localhost:8080/TP2 - CONNEXION/pages/page1.jsp" style="color:white;text-decoration:none;">Page 1</a> </td> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black; " > <a href="http://localhost:8080/TP2 - CONNEXION/pages/page2.jsp" style="color:white;text-decoration:none">Page 2</a> </td> <td style=" background-color:white; color:#404AA4; font-weight:bold; text-align:center" > Page 3 </td> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center; border:1px solid black" > <a href="http://localhost:8080/TP2 - CONNEXION/disconnect.do" style="color:white;text-decoration:none">Se déconnecter</a> </td> </tr> </thead> <tbody style=" background-color:#AAB0DF; height:150px; text-align:center; border:1px solid black " > <tr> <td colspan=4>Vous êtes sur la page 3.</td> </tr> </tbody> </table> </body> </html> |
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 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.awt.Color" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Erreur de connexion</title> </head> <body> <table style=" margin:auto; margin-top:50px; border-collapse:collapse; width:350px; text-align:right"> <thead> <tr> <td style=" background-color:#404AA4; color:white; font-weight:bold; text-align:center" >Erreur lors de la connexion ! </td> </tr> </tr> </thead> <tbody style=" background-color:#AAB0DF; border-radius-bottom-left:5px; border-radius-bottom-right:5px;" > <tr> <td style="text-align:left;padding-left:20px"> Des paramètres manquent afin de pouvoir vous connecter. Veuillez réessayer en cliquant sur <a href="connexion.jsp">ce lien</a>. </td> </tr> </tbody> </table> </body> </html> |
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 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.awt.Color" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>ERREUR 404 : page non trouvée</title> </head> <body> <% Color tab[] = { new Color(Color.lightGray.getRGB()), Color.gray, Color.darkGray, Color.black, Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.magenta, Color.cyan, Color.blue }; String titre = "ERREUR 404"; String message = "La page demandée n'est pas disponible ! "; char tabTitre[] = titre.toCharArray(); out.print("<h1>"); for(int i = 0, j = 0; i < tabTitre.length; i++, j++){ if(j == tab.length) j = 0; out.print("<span style=\"color:rgb(" + tab[j].getRed() + ", " + tab[j].getGreen() + ", " + tab[j].getBlue() + ")\">" + tabTitre[i] + "</span>"); } out.print("</h1>"); char tabMessage[] = message.toCharArray(); out.print("<p>"); for(int i = 0, j = 0; i < tabMessage.length; i++, j++){ if(j == tab.length) j = 0; out.print("<span style=\"color:rgb(" + tab[j].getRed() + ", " + tab[j].getGreen() + ", " + tab[j].getBlue() + ")\">" + tabMessage[i] + "</span>"); } out.print("</p>"); %> </body> </html> |
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 | package com.sdz.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class ConnexionServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String login = (String)request.getParameter("login"); String password = (String)request.getParameter("password"); HttpSession session = request.getSession(); session.setAttribute("login", login); session.setAttribute("password", password); //La gestion du cookie if(request.getParameter("auto_connect") != null){ System.out.println("Création des cookies."); Cookie loginCookie = new Cookie("login", login); Cookie passwordCookie = new Cookie("password", password); response.addCookie(loginCookie); response.addCookie(passwordCookie); } request.getRequestDispatcher("pages/page1.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ doGet(request, response); } } |
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 | package com.sdz.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DisconnectServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ request.getSession().invalidate(); request.getRequestDispatcher("connexion.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ doGet(request, response); } } |
1 2 3 4 5 6 7 8 9 10 11 | package com.sdz.exception; import javax.servlet.ServletException; public class ConnexionException extends ServletException { public ConnexionException(){} public ConnexionException(String message){ super(message); } } |
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 | package com.sdz.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; public class ConnexionFilter implements Filter{ private FilterConfig config; public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("Contrôle des identifiants de connexion."); //On contrôle l'existence de la session HttpSession session = ((HttpServletRequest)request).getSession(); if(session.getAttribute("login") != null && session.getAttribute("password") != null){ //Si on a des données en session, c'est qu'on est connecté chain.doFilter(request, response); } else{ String login = request.getParameter("login"); String password = request.getParameter("password"); //On vérifie ensuite si on a pas de formulaire de connexion validé if(login != null && password != null){ //Si on a des données en session, c'est qu'on est connecté System.out.println("Il y a des données : " + login + " - " + password + " !"); if(login.trim().equals("") || password.trim().equals("")) { throw new com.sdz.exception.ConnexionException("Vous devez renseigner au moins l'un des champs du formulaire"); } chain.doFilter(request, response); } else{ //Sinon, on retourne à la page de connexion System.out.println("Pas de session, on oriente donc vers la page de connexion."); request.getRequestDispatcher("/connexion.jsp").forward(request, response); } } } public void init(FilterConfig config) throws ServletException { this.config = config; } } |
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 | <web-app> <servlet> <servlet-class>com.sdz.servlet.ConnexionServlet</servlet-class> <servlet-name>connexionServlet</servlet-name> </servlet> <servlet> <servlet-class>com.sdz.servlet.DisconnectServlet</servlet-class> <servlet-name>deconnexionServlet</servlet-name> </servlet> <servlet-mapping> <servlet-name>connexionServlet</servlet-name> <url-pattern>/connexion.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>deconnexionServlet</servlet-name> <url-pattern>/disconnect.do</url-pattern> </servlet-mapping> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> <error-page> <exception-type>com.sdz.exception.ConnexionException</exception-type> <location>/error.jsp</location> </error-page> <filter> <filter-name>connexionFilter</filter-name> <filter-class>com.sdz.filter.ConnexionFilter</filter-class> </filter> <filter-mapping> <filter-name>connexionFilter</filter-name> <servlet-name>*</servlet-name> </filter-mapping> <welcome-file-list> <welcome-file>connexion.jsp</welcome-file> </welcome-file-list> </web-app> |

