随笔 - 217  文章 - 4  评论 - 4  阅读 - 23587

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>
复制代码

 

posted on   跨越&尘世  阅读(90)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示