定义一个login.jsp页面

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1>用户登录</h1><hr><% request.setCharacterEncoding("utf-8");String username="";String password = "";Cookie[] cookies = request.getCookies();if(cookies!=null&&cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")){username =  URLDecoder.decode(c.getValue(),"utf-8");}if(c.getName().equals("password")){password =  URLDecoder.decode(c.getValue(),"utf-8");}}}%><form name="loginForm" action="dologin.jsp" method="post"><table><tr><td>用户名:</td><td><input type="text" name="username" value="<%=username %>"/></td></tr><tr><td>密码:</td><td><input type="password" name="password" value="<%=password %>" /></td></tr><tr><td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天内记住我的登录状态</td></tr><tr><td colspan="2" align="center"><input type="submit" value="登录"/><input type="reset" value="取消"/></td></tr></table></form></body>
</html>

定义一个dologin的jsp页面

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'dologin.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1>登录成功</h1><hr><br><br><br><% request.setCharacterEncoding("utf-8");//首先判断用户是否选择了记住登录状态String[] isUseCookies = request.getParameterValues("isUseCookie");if(isUseCookies!=null&&isUseCookies.length>0){//把用户名和密码保存在Cookie对象里面String username = URLEncoder.encode(request.getParameter("username"),"utf-8");//使用URLEncoder解决无法在Cookie当中保存中文字符串问题String password = URLEncoder.encode(request.getParameter("password"),"utf-8");Cookie usernameCookie = new Cookie("username",username);Cookie passwordCookie = new Cookie("password",password);usernameCookie.setMaxAge(864000);passwordCookie.setMaxAge(864000);//设置最大生存期限为10天response.addCookie(usernameCookie);response.addCookie(passwordCookie);}else{Cookie[] cookies = request.getCookies();if(cookies!=null&&cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")||c.getName().equals("password")){c.setMaxAge(0); //设置Cookie失效response.addCookie(c); //重新保存。}}}}%><a href="users.jsp" target="_blank">查看用户信息</a></body>
</html>

定义一个user的jsp页面

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'users.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1>用户信息</h1><hr><% request.setCharacterEncoding("utf-8");String username="";String password = "";Cookie[] cookies = request.getCookies();if(cookies!=null&&cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")){username = URLDecoder.decode(c.getValue(),"utf-8");}if(c.getName().equals("password")){password = URLDecoder.decode(c.getValue(),"utf-8");}}}%><BR><BR><BR>用户名:<%=username %><br>密码:<%=password %><br></body>
</html>

运行结果

68 cookie在登录中的作用相关推荐

  1. 【_ 面試 】在单点登录中,如果 cookie 被禁用了怎么办?

    在单点登录中,如果 cookie 被禁用了怎么办? 单点登录的原理是后端生成一个 session ID,然后设置到 cookie,后面的所有请求浏览器都会带上 cookie,然后服务端从 cookie ...

  2. 在单点登录中,如果cookie被禁用了怎么办?

    在单点登录中,如果cookie被禁用了怎么办? 单点登录的原理是后端生成一个 session ID,然后设置到 cookie,后面的所有请求浏览器都会带上 cookie,然后服务端从 cookie 里 ...

  3. COOKIE伪造登录网站后台

    1.关于XSS(跨站脚本攻击)和CSRF(跨站请求伪造)的知识,xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从 ...

  4. java session 永不过期_Java Web Application使Session永不失效(利用cookie隐藏登录)

    在做 Web Application 时,因为 Web Project 有 session 自动失效的问题,所以如何让用户登录一次系统就能长时间运行三个月,就是个问题. 后来,看到 session 失 ...

  5. 汇编在嵌入式编程中的作用_如何在嵌入式Power BI报表中以编程方式传递凭据

    汇编在嵌入式编程中的作用 In the article, How to embed a Power BI Report Server report into an ASP.Net web applic ...

  6. php curl post登录与带cookie模拟登录随笔

    研究curl  post登录 一步一步来,一个星期前我也不知道curl是啥.都是百度学习的,好了贴代码 这个是没有验证码的登录(账号密码自己申请一个) <?php $cookieSuccess ...

  7. WCF技术剖析之十五:数据契约代理(DataContractSurrogate)在序列化中的作用

    如果一个类型,不一定是数据契约,和给定的数据契约具有很大的差异,而我们要将该类型的对象序列化成基于数据契约对应的XML.反之,对于一段给定的基于数据契约的XML,要通过反序列化生成该类型的对象,我们该 ...

  8. Python爬虫——Cookie模拟登录

    文章目录 Python爬虫--Cookie模拟登录 1.Cookie模拟登录 2.Handler处理器 Python爬虫--Cookie模拟登录 1.Cookie模拟登录 现在很多网站需要用户成功登录 ...

  9. Movement Disorders脑电格兰杰因果分析:运动皮质在帕金森病复发性震颤中的作用

    帕金森患者在手臂伸展时可能会出现不同程度的复发性震颤.来自罗马大学的Alfredo Berardelli等人在MovementDisorders发文,旨在探讨初级运动皮质在复发性震颤中的作用,并与静止 ...

最新文章

  1. word技巧 输入方框中带对勾的符号的快捷方式
  2. 【400】numpy.pad 为数组加垫(迷宫类题目)
  3. pluto实现分析(7)
  4. 【Android工具】最新测试谷歌play耗电情况,各种品牌安装谷歌play方法,GooglePlay...
  5. Github标星1.6W+,程序员不得不知的“潜规则”又火了,早知道就不会秃头了
  6. [整理]Git使用文章整理
  7. 有序数组中插入元素依然保持有序
  8. Daily Scrum 10.28
  9. 喜庆:上周阅读量5W,超过了99%的C友
  10. 《Frustum PointNets for 3D Object Detection from RGB-D Data》论文及代码学习(二)——代码部分
  11. css技巧---电子表体字体引入
  12. 风尚云网学前端----HTML特殊字符编码对照表
  13. 3Dmax各类问题汇总及其完整解决方法
  14. div垂直居中和水平居中的多种方式
  15. ADSL家庭宽带用户提高迅雷下载速度的几个实用方法
  16. Latex 使用\begin{aligned} 出现 Environment aligned undefined.
  17. jeefast 添加非空判断
  18. landesk桌面管理服务器安装篇
  19. kali中的firefox无法打开:your tab just crashed
  20. .Scrum团队成立

热门文章

  1. 408计算机组成原理有汇编吗,2021考研408计算机组成原理习题:计算机系统概述
  2. linux 网络配置 阮一峰,Vim 配置入门
  3. epoll哪些触发模式_5.epoll的水平触发和边缘触发
  4. 多租户saas 架构_[译/注] Force.com 多租户互联网应用开发平台的设计
  5. 【51单片机快速入门指南】5:软件SPI
  6. C++ 控制结构和函数(一) —— 控制结构
  7. 将windows下编辑好的文件(GBK)转换成Linux下的格式(UTF8)
  8. vue中如何设置和清除定时器setInterval
  9. [ECMAScript] 说说你对class的理解
  10. [react] 请说下react组件更新的机制是什么