Cookie实现十天内免登录

  • 代码:
   - CheckLogin 查看是否获取到了Cookie,如果获取到了连接数据库验证Cookie发过来的用户名和密码,如果没有获取到Cookie信息,那么就跳转到登录页

      import javax.servlet.ServletException;
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;
import java.sql.*; public class CheckLogin extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //定义需要的变量
String userEmail=null;
String userPwd=null;
String userName=null;
boolean loginFlag=false; //获取浏览器提交给服务器的Cookie信息
Cookie[] cookies=request.getCookies(); //遍历Cookie数组
if(cookies!=null){
for (Cookie cookie:cookies) {
String cookieName=cookie.getName();
String cookieValue=cookie.getValue();
if(cookieName.equals("userEmail")){
userEmail=cookieValue;
System.out.println(userEmail);
}else if(cookieName.equals("userPwd")){
userPwd=cookieValue;
System.out.println(userPwd);
}
}
}else{
response.sendRedirect("/myWeb/login.html"); } if(userEmail!=null && userPwd!=null) {
//创建数据库连接对象
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null; try { // 连接数据库
Class.forName("com.mysql.cj.jdbc.Driver"); // 获取连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai",
"root", "123456"); // 获取预编译数据库操作对象
String sql = "select username,email,userpwd from t_userInfo where email=? and userpwd=?";
preparedStatement = connection.prepareStatement(sql); //给预编译语句传参
preparedStatement.setString(1, userEmail);
preparedStatement.setString(2, userPwd); //执行SQL语句
resultSet = preparedStatement.executeQuery(); //处理查询结果集
while (resultSet.next()) {
loginFlag = true;
userName = resultSet.getString("username");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally { //释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//设置字符集,获取字符输入流
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
if (loginFlag) {
//验证Cookie发过来的邮箱密码都正确
//HTML代码
out.print(" <!DOCTYPE html>");
out.print("<meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/> ");
out.print(" <head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>用户信息</title>");
out.print(" </head>");
out.print(" <body>");
out.print(" <h3 align='center'>登陆成功,欢迎" + userName + "</h3>");
out.print(" </body>");
out.print(" </html>");
} else { //登录失败
//HTML代码
out.print(" <!DOCTYPE html>");
out.print("<meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/> ");
out.print(" <head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>用户信息</title>");
out.print(" </head>");
out.print(" <body>");
out.print(" <h3 align='center'>密码错误,请重试");
out.print(" <a href='/myWeb/login.html'>重新登录</a>");
out.print(" </body>");
out.print(" </html>"); }
}
}else{
response.sendRedirect("/myWeb/login.html");
}
} }
  • 登录页, 获取用户在表单中输入的用户名和密码,连接数据库验证用户名和密码, 验证用户名和密码正确的话,继续验证用户是否勾选了免登录的复选框,如果勾选了,创建cookie,并将cookie发送给浏览器。

               package com.shige.controller;
    
          import javax.servlet.ServletException;
    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;
    import java.sql.*; public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //解决中文乱码问题
    request.setCharacterEncoding("UTF-8"); //获取用户提交的表单数据
    String userEmail=request.getParameter("email");
    String userPwd=request.getParameter("password");
    //创建数据库连接对象
    Connection connection=null;
    PreparedStatement preparedStatement=null;
    ResultSet resultSet=null;
    boolean loginFlag=false;
    String userName=null; try { // 连接数据库
    Class.forName("com.mysql.cj.jdbc.Driver"); // 获取连接
    connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai",
    "root","123456"); // 获取预编译数据库操作对象
    String sql="select username,email,userpwd from t_userInfo where email=? and userpwd=?";
    preparedStatement=connection.prepareStatement(sql); //给预编译语句传参
    preparedStatement.setString(1,userEmail);
    preparedStatement.setString(2,userPwd); //执行SQL语句
    resultSet=preparedStatement.executeQuery(); //处理查询结果集 while(resultSet.next()){
    loginFlag=true;
    userName=resultSet.getString("username");
    } } catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace();
    }finally { //释放资源
    if(resultSet!=null){
    try {
    resultSet.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(preparedStatement!=null){
    try {
    preparedStatement.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } if(connection!=null){
    try {
    connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } if(loginFlag){
    //登录成功
    //登录成功够查看用户是否选择了自动登录的按钮
    String tenDayAutoLogin=request.getParameter("tenDayAutoLogin");
    System.out.println(tenDayAutoLogin);
    if("ok".equals(tenDayAutoLogin)){
    //说明用户勾选了十天内免登录的选项
    //创建Cookie对象
    Cookie cookie1=new Cookie("userEmail",userEmail);
    Cookie cookie2=new Cookie("userPwd",userPwd); //设置Cookie有效时间
    cookie1.setMaxAge(60*60*24*10);
    cookie2.setMaxAge(60*60*24*10); //设置Cookie关联路径
    cookie1.setPath(request.getContextPath());
    cookie2.setPath(request.getContextPath()); //将Cookie对象发送给浏览器
    response.addCookie(cookie1);
    response.addCookie(cookie2);
    }
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out=response.getWriter();
    //HTML代码
    out.print(" <!DOCTYPE html>");
    out.print("<meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/> ");
    out.print(" <head>");
    out.print(" <meta charset='UTF-8'>");
    out.print(" <title>用户信息</title>");
    out.print(" </head>");
    out.print(" <body>");
    out.print(" <h3 align='center'>登陆成功,欢迎"+userName+"</h3>");
    out.print(" </body>");
    out.print(" </html>");
    }else{
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out=response.getWriter();
    //登录失败
    //HTML代码
    out.print(" <!DOCTYPE html>");
    out.print("<meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/> ");
    out.print(" <head>");
    out.print(" <meta charset='UTF-8'>");
    out.print(" <title>用户信息</title>");
    out.print(" </head>");
    out.print(" <body>");
    out.print(" <h3 align='center'>密码错误,请重试");
    out.print(" <a href='/myWeb/login.html'>重新登录</a>");
    out.print(" </body>");
    out.print(" </html>"); }
    } }
    }

JAVAEE_Servlet_22_Cookie实现十天内免登录的更多相关文章

  1. Spring Security框架下实现两周内自动登录"记住我"功能

    本文是Spring Security系列中的一篇.在上一篇文章中,我们通过实现UserDetailsService和UserDetails接口,实现了动态的从数据库加载用户.角色.权限相关信息,从而实 ...

  2. JavaWeb 08_JSP+Dao+Bean+Servlet 实现登录注册(连接数据库,验证码登录,两周内免登陆等功能)

    一.数据库db_01   表usert   字段username,password 二. 目录 三. 配置信息 四. 代码 index.jsp <script type="text/j ...

  3. spring boot 实现密码连续输入错误5次,限制十分钟内不能进行登录

    我们要实现的就是,密码连续输入错误5次,就限制用户十分钟不能进行登录. 大致的流程图 数据库设计如下 DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ...

  4. SourceTree 免登录跳过初始设置 - 转

    转自:http://www.cnblogs.com/xiofee/p/sourcetree_pass_initialization_setup.html 在SourceTree的配置目录新建(或修改) ...

  5. 火车采集器 帝国CMS7.2免登录发布模块

    帝国cms7.2增加了金刚模式,登录发布有难度.免登录发布模块配合火车采集器,完美解决你遇到的问题. 免登录直接获取栏目列表 通过文件内设置密码免登录发布数据 帝国cms7.2免登陆文章发布接口使用说 ...

  6. django 实现同一个ip十分钟内只能注册一次

    很多小伙伴都会有这样的问题,说一个ip地址十分钟内之内注册一次,用来防止用户来重复注册带来不必要的麻烦 逻辑: 取ip,在数据库找ip是否存在,存在判断当前时间和ip上次访问时间之差,小于600不能注 ...

  7. django 实现同一个ip十分钟内只能注册一次(redis版本)

    上一篇文章,django 实现同一个ip十分钟内只能注册一次 的时候,我们在注册的时候选择使用的使我们的数据库来报错我们的注册的ip信息,可是如果数据量大,用户多的时候,单单靠我们的数据库 来储存我们 ...

  8. js之10天内免登陆

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 基于 Laravel-Admin 在十分钟内搭建起功能齐全的后台模板

    http://laravelacademy.org/post/6468.html 1.简介 为 Laravel 提供后台模板的项目越来越多,学院君已陆续为大家介绍过Laravel Angular Ad ...

随机推荐

  1. [计算机图形学]视图变换:MVP变换、视口变换

    目录 一.MVP变换 1. 模型变换 1.1 缩放矩阵 1.2 旋转矩阵 1.3 平移矩阵 2. 视角变换 3. 投影变换 二.Viewport变换 一.MVP变换 MVP变换是模型变换(M).视角变 ...

  2. MySQL 事务的隔离级别

    转载:https://developer.aliyun.com/article/743691?accounttraceid=80d4fddb3dc64b97a71118659e106221tozz 简 ...

  3. linux之安装nginx

    nginx官网:http://nginx.org/en/download.html 1.安装nginx所需环境 a)  PCRE pcre-devel 安装 # yum install -y pcre ...

  4. 基于docker搭建gitlab

    一.概述 GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. 它拥有与Github类似的功能,能够浏览 ...

  5. 报错: You are using pip version 10.0.1, however version 18.0 is available.

    报错: You are using pip version 10.0.1, however version 18.0 is available. You should consider upgradi ...

  6. JWT原理及常见攻击方式

    JWT的全称是Json Web Token.它遵循JSON格式,将用户信息加密到token里,服务器不保存任何用户信息,只保存密钥信息,通过使用特定加密算法验证token,通过token验证用户身份. ...

  7. 10. vue之webpack打包详解

    一.什么是webpack webpack官网给出的定义是 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应 ...

  8. 基于Hi3559AV100的视频采集(VDEC-VPSS-VO)整体框图设计

    下面给出基于Hi3559AV100的视频采集整体设计,具体设计将在后续给出: 图形采集端整体设计 Hi3559AV100软件程序按结构划分可分为4层,第一层是硬件驱动层,第二层是操作系统层,第三层是媒 ...

  9. WPF中Popup上的textbox无法切换到中文输入法

    As Marco Zhou has said in the msdn forum (http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b ...

  10. Java 重入锁和读写锁

    本文部分摘自<Java 并发编程的艺术> 重入锁 重入锁 ReentrantLock,顾名思义,就是支持重进入的锁,它表示该锁能够支持一个线程对资源的重复加锁.除此之外,该锁还支持获取锁时 ...