Cookies记住用户名密码(免登陆)

  1. 首页(登录页面)
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/8/8
  Time: 14:19
  To change this template use File | Settings | File Templates.

  Parameter:参数   character:特性  encoding:编码  content:内容
  求精要诀P101
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Cookies测试首页</title>
</head>
<body> 
<%! String uname;
    String upwd;

%> 
<%
    Cookie cookie=null;
    Cookie [] cookies=null;

    //获取cookies数组
    cookies=request.getCookies();
    //遍历查询

    if(cookies !=null)
    {
        System.out.println(cookies.length);
        for(int i=0;i<cookies.length;i++)
        {
            System.out.println("001"+cookies[i].getName() +cookies[i].getValue());
            //username=cookies[i].getValue();
            if (cookies[i].getName().equals("pwd"))
            {
                upwd=cookies[i].getValue();

            }
            if(cookies[i].getName().equals("name"))
            {
                uname=cookies[i].getValue();
            }
        }
    }
    else
    {
        uname="  ";
    }

%>  
<form action="SetCookiesServlet" method="post">
    用户名:<input type="text" name="username" value="<%=uname%>"> <br>
    密码:<input type="text" name="userpwd" value="<%=upwd%>" ><br>
    <input type="submit" value="提交">

</form>
</body>
</html>

2、设置Cookies



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet( "/SetCookiesServlet")
public class SetCookiesServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        //响应内容类型编码设置
        response.setContentType("text/html;charset=UTF-8");
        //获取用户名和密码参数
        String username = request.getParameter("username");
        String userpwd = request.getParameter("userpwd");
        //创建Cookies
        Cookie name = new Cookie("name" , username);
        Cookie pwd = new Cookie("pwd", userpwd);
        //设置Cookies的有效时间,单位是秒
        name.setMaxAge(30*60);
        pwd.setMaxAge(30*60);
        //在响应头中添加两个Cookies
        response.addCookie(name);
        response.addCookie(pwd);
        //响应页面输出
        PrintWriter out = response.getWriter();

        //Cookies中存放的值得形式
        //姓名javax.servlet.http.Cookie@687766ec
        //密码javax.servlet.http.Cookie@2d75e586

        //out.println("姓名"+name+"<br>");
        //out.println("密码"+pwd+"<br>");


        //参数形式
       // System.out.println("姓名"+username+"<br>");
       // System.out.println("密码"+userpwd+"<br>");
		//进行响应(不一定非要响应到原页面,只要是响应到客户端就行)
        response.sendRedirect("index.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }
}

3、校验(记住用户名和密码)

传入首页,在用户登陆之前进行校验
<% !全局变量%>
<%= 输出语句%>
<% java代码%>

<%! String uname;
    String upwd;

%>
<%
    Cookie cookie=null;
    Cookie [] cookies=null;

    //获取cookies数组
    cookies=request.getCookies();
    //遍历查询
//当cookies失效或者是没有cookies的时候,赋值为空
    if(cookies !=null)
    {
        System.out.println(cookies.length);
        for(int i=0;i<cookies.length;i++)
        { 
             if (cookies[i].getName().equals("pwd"))
            {
                upwd=cookies[i].getValue();
				//cookies是由键值对存在的,调用方法获取键和值。
            }
            if(cookies[i].getName().equals("name"))
            {
                uname=cookies[i].getValue();
            }
        }
    }
    else
    {
        uname="  ";
    }

在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值