jsp——Cookie实现免输用户名和密码
直接上代码
<%@page import="java.net.URLEncoder"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="check.jsp" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" size="20" name="username" value="${cookie.username.value }"></td> </tr> <tr> <td>密码:</td> <td><input type="password" size="20" name="passwd" value="${cookie.passwd.value }"></td> </tr> </table> <button type="submit">登录</button> <button type="reset">重置</button> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>后台处理</title> </head> <body> <% String username=request.getParameter("username"); String passwd=request.getParameter("passwd"); if("admin".equals(username)&&"123456".equals(passwd)){ //登录成功 Cookie c1=new Cookie("username",username); Cookie c2=new Cookie("passwd",passwd); c1.setMaxAge(60); c2.setMaxAge(60); response.addCookie(c1); response.addCookie(c2); response.getWriter().append("登陆成功"); response.setHeader("refresh", "2,url=index.jsp"); }else{ //登录失败 response.getWriter().append("登陆失败"); response.setHeader("refresh", "2,url=login.jsp"); } %> </body> </html>