Servlets: Créer une page de connexion qui mène à une nouvelle page
import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;
public class FormLogin extends HttpServlet {
public static String USER_KEY = "ServletLogin.user";public class FormLogin extends HttpServlet {
public static String FIELD_USER = "username";
public static String FIELD_PASSWORD = "password";
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
java.io.IOException { resp.setContentType("text/html"); java.io.PrintWriter out = resp.getWriter(); resp.setHeader("Expires", "Tues, 01 Jan 2016 00:00:00 GMT"); String uri = req.getRequestURI();
HttpSession session = req.getSession(); String user = (String) session.getAttribute(USER_KEY);
if (user == null) { login(out, uri); return; } out.println("<html>"); out.println("<head>"); out.println("<title>Bienvenu</title>"); out.println("</head>"); out.println("<body>"); out.println("<center><h2>Bienvenu sur notre site!</h2>"); out.println("</center><br><br>"); out.println("</body>"); out.println("</html>"); out.flush(); }
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { resp.setContentType("text/html"); java.io.PrintWriter out = resp.getWriter(); HttpSession session = req.getSession(true); String user = (String) session.getAttribute(USER_KEY); if (user == null) { String username = req.getParameter(FIELD_USER); String password = req.getParameter(FIELD_PASSWORD); if (!validUser(username, password)) { out.println("<html>"); out.println("<title><Erreur de connexion</title>"); out.println("<body><center><h2>" + "Nom d'utilisateur incorrecte!</h2><br>"); out.println("veuillez rééssayer"); out.println("</center></body></html>"); out.flush(); return; } session.setAttribute(USER_KEY, username); } resp.sendRedirect(req.getRequestURI()); } protected void login(java.io.PrintWriter out, String uri) throws java.io.IOException { out.println("<html>"); out.println("<head>"); out.println("<title>Page de Connexion</title>"); out.println("<center><h2>Bienven, veuillez vous identifier</h2>"); out.println("<br><form method=POST action="" + uri + "">"); out.println("<table>"); out.println("<tr><td>Nom d'utilisateur:</td>"); out.println("<td><input type=text name=" + FIELD_USER + " size=30></td></tr>"); out.println("<tr><td>Mot de passe:</td>"); out.println("<td><input type=password name=" + FIELD_PASSWORD + " size=10></td></tr>"); out.println("</table><br>"); out.println("<input type=submit value="Connexion">"); out.println("</form></center></body></html>"); } protected boolean validUser(String username, String password) { boolean valid = false; if ((username != null) && (username.length() > 0)) { valid = username.equals(password); } return valid; } }public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
java.io.IOException { resp.setContentType("text/html"); java.io.PrintWriter out = resp.getWriter(); HttpSession session = req.getSession(true); String user = (String) session.getAttribute(USER_KEY);
if (user == null) { String username = req.getParameter(FIELD_USER); String password = req.getParameter(FIELD_PASSWORD);
if (!validUser(username, password)) { out.println("<html>"); out.println("<title><Erreur de connexion</title>"); out.println("<body><center><h2>" + "Nom d'utilisateur incorrecte!</h2><br>"); out.println("veuillez rééssayer"); out.println("</center></body></html>"); out.flush(); return; } session.setAttribute(USER_KEY, username); } resp.sendRedirect(req.getRequestURI()); }
protected void login(java.io.PrintWriter out, String uri) throws java.io.IOException { out.println("<html>"); out.println("<head>"); out.println("<title>Page de Connexion</title>"); out.println("<center><h2>Bienven, veuillez vous identifier</h2>"); out.println("<br><form method=POST action="" + uri + "">"); out.println("<table>"); out.println("<tr><td>Nom d'utilisateur:</td>"); out.println("<td><input type=text name=" + FIELD_USER + " size=30></td></tr>"); out.println("<tr><td>Mot de passe:</td>"); out.println("<td><input type=password name=" + FIELD_PASSWORD + " size=10></td></tr>"); out.println("</table><br>"); out.println("<input type=submit value="Connexion">"); out.println("</form></center></body></html>"); }
protected boolean validUser(String username, String password) { boolean valid = false; if ((username != null) && (username.length() > 0)) { valid = username.equals(password); }
return valid; }}
Aucun commentaire:
Enregistrer un commentaire