jsp——Cookie实现免输用户名和密码

2022/4/14 23:14:12

本文主要是介绍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>

 



这篇关于jsp——Cookie实现免输用户名和密码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程