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 ...
随机推荐
- Elasticsearch---DSL搜索实践
Domain Specific Language 特定领域语言,基于JSON格式的数据查询,查询更灵活,有利于复杂查询 一.普通url路径参数搜索 数据准备 1.建立名字为 shop 的索引 2.手动 ...
- 微信小程序:优化接口代码-提取公共接口路径
方法一.将公共部分提取出来定义为baseURL变量 简化url,把里面公共部分提取出来.如https://api-hmugo-web.itheima.net/api/public/v1/categor ...
- ThreadPoolExecutor中execute和submit的区别
1:入参不同 excute() 传入的是 Runable, submit 传入的是 Callable 或 Runable 1):execute 方法源码 public void execute(Run ...
- 后端程序员之路 23、一个c++的api framework
在"21.一个cgi的c++封装"中,我们封装了cgi,在这之上,我们可以再来封装一个webapi的framework.当然,前文的Casablanca是个不错的选择,但是它比较庞 ...
- 1.2 Python3基础-规范
>>返回主目录 总的来说,如果安装的不是安装的Anaconda,pip命令还是经常会用到的(cmd模式使用),当然也可以在PyCharm中直接安装 PEP8规范,我另有一篇博客已经写好,可 ...
- sql之对top语句理解
起因 事情的起因是在群里和大佬讨论问题,讨论select top x后加*的问题,这时突发奇想,想搞清楚这个看起来在sql中略显突兀的语句结构,就有了这篇文 先看例子 select * from 表 ...
- FreeBSD 12.2 已经发布 从现有版本更新到12
#freebsd-update -r 12.2-RELEASE upgrade 如果提示更新第三方软件后,再执行freebsd-update install , 请输入 #pkg update &am ...
- 解决图片把父元素向下撑大大约3px问题
现象 bug: 图片在div\li\dt 等 图片把父元素向下撑大大约3px <style> img { width: 30%; //这里由于 ...
- Codeforces Round #557 B. Double Matrix
题面: 传送门 题目描述: 给出两个n*m的矩阵,问:是否能通过交换两个矩阵"对应"位置的元素,使两个矩阵都为"递增"矩阵. "递增"矩阵定 ...
- c++ 反汇编 异常处理
c++异常处理 int main(){ try { throw 1; } catch ( int e ) { printf("catch int\r\n"); } catch ( ...