login.jsp
<%@page import="sun.security.util.Password"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String action=request.getParameter("action"); String User=null; String Password=null; if("login".equals(action)) { //获取参数 User=request.getParameter("username"); Password=request.getParameter("password"); int Timeout=new Integer(request.getParameter("timeout")); //新建Cookie,并设置有效期 Cookie accountCookie=new Cookie("account", User); accountCookie.setMaxAge(Timeout); String infoString=User+Password; Cookie infoCookie=new Cookie("info", infoString); infoCookie.setMaxAge(Timeout); //输出到客户端 response.addCookie(accountCookie); response.addCookie(infoCookie); //重新请求本页面 response.sendRedirect(request.getRequestURI()+"?"+System.currentTimeMillis()); return; }else if("logout".equals(action)) { Cookie accountCookie=new Cookie("account", ""); accountCookie.setMaxAge(0); Cookie infoCookie=new Cookie("info", ""); infoCookie.setMaxAge(0); response.addCookie(accountCookie); response.addCookie(infoCookie); response.sendRedirect(request.getRequestURI()+"?"+System.currentTimeMillis()); return; } boolean login=false; String account=null; String info=null; if(request.getCookies()!=null ) { for(Cookie cookie:request.getCookies()) { if(cookie.getName().equals("account")) account=cookie.getValue(); if(cookie.getName().equals("info")) info=cookie.getValue(); } } if(account !=null && info !=null) { login=info.equals(account+"123"); } %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>西南石油大学电子邮件系统</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <% if(login){ %> 你已经登录,${cookie.account.value }. <a href="${pageContext.request.requestURI }?action=logout">注销 </a> <%}else{ %> <div class="top"> <div class="r1"> <div class="r2"> <div class="logo"></div> </div> <a href="" target="" class="help">帮助</a> </div> </div> <div class="content"> <div class="loginBar"> <div class="box"> <div class="tab"> 账号登录 <div class="dragbar"></div> </div> </div> <div class="boxc"> <div style="height: 10px;"></div> <div style="margin-left: 42px; width: 270px; height: 30px;"> <div class="hh" id="hd">用户登录</div> </div> <form action="${pageContext.request.requestURI}?action=login" method="post" onsubmit="return ajax()" > <input type="text" class="text" name="username" id="username" style="ime-mode: disabled" _autocomplete="off" placeholder="用户名" /> <input type="password" class="text" name="password" id="password" _autocomplete="off" placeholder="密码" /> <div style="height: 10px;"></div> <div class="bl"> <span style="float: left;"> <input type="radio" name="timeout" value="<%=30*24*60*60 %>" checked> <font style="color: red; font-family: 宋体; clear: both;">保存登录信息,30天内有效</font> </span> <span style="float: left;"> <input type="radio" name="timeout" value="-1"> <font style="color: red; font-family: 宋体; clear: both;">不保存登录信息</font> </span> </div> <input type="submit" class="btn" value="登 录" style="background: url(img/login_btn.jpg)" /> </form> </div> </div> </div> <div class="bottom">西南石油大学</div> <%} %> </body> </html>
原文:https://www.cnblogs.com/h2503652646/p/10651903.html