JAVAEE_Servlet_22_Cookie实现十天内免登录
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实现十天内免登录的更多相关文章
- Spring Security框架下实现两周内自动登录"记住我"功能
本文是Spring Security系列中的一篇.在上一篇文章中,我们通过实现UserDetailsService和UserDetails接口,实现了动态的从数据库加载用户.角色.权限相关信息,从而实 ...
- JavaWeb 08_JSP+Dao+Bean+Servlet 实现登录注册(连接数据库,验证码登录,两周内免登陆等功能)
一.数据库db_01 表usert 字段username,password 二. 目录 三. 配置信息 四. 代码 index.jsp <script type="text/j ...
- spring boot 实现密码连续输入错误5次,限制十分钟内不能进行登录
我们要实现的就是,密码连续输入错误5次,就限制用户十分钟不能进行登录. 大致的流程图 数据库设计如下 DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ...
- SourceTree 免登录跳过初始设置 - 转
转自:http://www.cnblogs.com/xiofee/p/sourcetree_pass_initialization_setup.html 在SourceTree的配置目录新建(或修改) ...
- 火车采集器 帝国CMS7.2免登录发布模块
帝国cms7.2增加了金刚模式,登录发布有难度.免登录发布模块配合火车采集器,完美解决你遇到的问题. 免登录直接获取栏目列表 通过文件内设置密码免登录发布数据 帝国cms7.2免登陆文章发布接口使用说 ...
- django 实现同一个ip十分钟内只能注册一次
很多小伙伴都会有这样的问题,说一个ip地址十分钟内之内注册一次,用来防止用户来重复注册带来不必要的麻烦 逻辑: 取ip,在数据库找ip是否存在,存在判断当前时间和ip上次访问时间之差,小于600不能注 ...
- django 实现同一个ip十分钟内只能注册一次(redis版本)
上一篇文章,django 实现同一个ip十分钟内只能注册一次 的时候,我们在注册的时候选择使用的使我们的数据库来报错我们的注册的ip信息,可是如果数据量大,用户多的时候,单单靠我们的数据库 来储存我们 ...
- js之10天内免登陆
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 基于 Laravel-Admin 在十分钟内搭建起功能齐全的后台模板
http://laravelacademy.org/post/6468.html 1.简介 为 Laravel 提供后台模板的项目越来越多,学院君已陆续为大家介绍过Laravel Angular Ad ...
随机推荐
- 小记一下递归通过id寻找一条链路的数据
Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.sp ...
- http server源码解析
本文主要过下http生成服务和处理请求的主要流程,其他功能并未涉及. 使用例子 const http = require('http'); http.createServer((req, res) = ...
- 低功耗蓝牙 ATT/GATT/Service/Characteristic 规格解读
什么是蓝牙service和characteristic?如何理解蓝牙profile? ATT和GATT两者如何区分?什么是attribute? attribute和characteristic的区别是 ...
- 后端程序员之路 18、朴素贝叶斯模型(Naive Bayesian Model,NBM)
贝叶斯推断及其互联网应用(一):定理简介 - 阮一峰的网络日志http://www.ruanyifeng.com/blog/2011/08/bayesian_inference_part_one.ht ...
- Pyqt5——带图标的表格(Model/View)
需求:表格中第一列内容为学生学号,为了突出学号的表示,在第一列的学号旁增加学号图标. 实现:(1)使用Qt的model-view模式生成表格视图. (2)重写代理(QAbstractItemDeleg ...
- Elastic App Search 快速构建 ES 应用
公号:码农充电站pro 主页:https://codeshellme.github.io App Search 是 Elastic 家族中的一个产品,它可以帮助我们(基于 ES)快速高效的构建搜索应用 ...
- 死磕Spring之IoC篇 - @Bean 等注解的实现原理
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- External Libraries中没有Maven的jar包的原因(已解决)
**深坑!** ## External Libraries中没有Maven的jar包的原因(已解决) 2021年3月1日 --- 搭建一个新项目 IDEA 从 Git 上拉 拉去Maven项目然后 m ...
- nginx使用-1(源码安装nginx)
Nginx概述 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Ramb ...
- Django中间件(中间件版登陆验证、访问频率限制)
一.介绍 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的功能. ...