1、userLogin页面
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/10/15 Time: 12:03 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录</title> </head> <body> // 登录页面读取Cookie <% Cookie [] cookies = request.getCookies(); String username = ""; for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("name")) { username = cookies[i].getValue(); } } %> <form action="loginSuccess.jsp" method="post"> <table > <tr> <td>用户名 :<input type="text" name="username" value="<%=username%>" required></td> </tr> <tr> <td>密 码 :<input type="password" name="password" required></td> </tr> <tr> <td> <input type="submit" value="登录"> <input type="reset" value="重置"> </td> </tr> </table> </form> <% // 设置请求的编码方式 request.setCharacterEncoding("UTF-8"); %> </body> </html>
2、loginSuccess页面
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/10/15 Time: 12:04 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录后页面</title> </head> <body> <% String name=request.getParameter("username");// 取得 name 的信息 String pwd=request.getParameter("password"); // 取得 pwd 的信息 %> <p>姓名:<%=name%></p> <p>密码:<%=pwd%></p> <% boolean isSuccess=false; // 用户名:admin 密码:123 // 进行用户名和密码的验证 if(name.equals("admin") && pwd.equals("123")){ isSuccess=true; // setAttribute方法 对request对象的属性进行保存 request.setAttribute("msg","恭喜管理员大人登录成功!"); // 登录成功用户名和密码保存在session中 session.setAttribute("uName",name); session.setAttribute("uPWD",pwd); // 添加到cookie Cookie cookie = new Cookie("name", name); // 设置有效期1天 cookie.setMaxAge(60*60*24); // 添加到cookie response.addCookie(cookie); // 跳转至logout.jsp页面中 //response.sendRedirect("logout.jsp"); response.setHeader("refresh", "2; URL = logout.jsp"); // 定时2秒跳转 }else { request.setAttribute("msg","登入失败!"); // 转发到userLogin.jsp页面中 request.getRequestDispatcher("userLogin.jsp").forward(request,response); } // getAttribute方法 对request对象的属性进行读取 String result=(String)request.getAttribute("msg"); %> <h3><%=result%></h3> <%--<input type="text" value=<%=session.getAttribute("uName")%> readonly>--%> </body> </html>
3、logout页面
<%-- Created by IntelliJ IDEA. User: mu_bai Date: 2019/10/15 Time: 19:41 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注销页面</title> </head> <body> <% String name = (String) session.getAttribute("uName"); String pwd = (String) session.getAttribute("uPWD"); String result = (String) session.getAttribute("msg"); if (name != null || pwd != null || result != null) { %> <p>用户名:<%=name%></p> <p>密码:<%=pwd%></p> <%} //注销 session session.invalidate(); %> <form action="userLogin.jsp" method="post"> <input type="submit" name="注销" value="注销"> </form> </body> </html>