Cookie与Session用法
以下是Cookie的完整实例
MyEclipse新建web Project工程,建两个jsp文件,如下
1、login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
if("POST".equals(request.getMethod())){
Cookie usernameCookie=new Cookie("username",request.getParameter("username"));//新建名为username的Cookie
Cookie visittimesCookie=new Cookie("visitTimes",""); //新建Cookie
response.addCookie(usernameCookie);//添加到response中
response.addCookie(visittimesCookie);//response会将Cookie发送给客户端 response.sendRedirect(request.getContextPath()+"/cookie.jsp");//显示Cookie页面
return;
} %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>请先登录</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div align="center" style="margin:10px; ">
<fieldset>
<legend>登录</legend>
<form action="login.jsp" method="post">
<table> <tr>
<td>帐号:</td>
<td>
<input type="text" name="username" style="width:200px; ">
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input type="password" name="password" style="width:200px; ">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value=" 登 录 " class="button">
</td>
</tr>
</table>
</form>
</fieldset>
</div> </body>
</html>
2、cookie.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="login.jsp" %> <%
request.setCharacterEncoding("UTF-8");
String username=""; //用户名
int visitTimes=; //访问次数 Cookie[] cookies=request.getCookies();//所有的Cookie
for(int i=;i<cookies.length;i++){
Cookie cookie=cookies[i]; //第i个Cookie
if("username".equals(cookie.getName())){
username=cookie.getValue();
}
else if("visitTimes".equals(cookie.getName())){
visitTimes=Integer.parseInt(cookie.getValue());
}
}
if(username==null||username.trim().equals("")){
throw new Exception("还没登陆,请先登录");
} Cookie visitTimesCookie=new Cookie("visitTimes",Integer.toString(++visitTimes));
response.addCookie(visitTimesCookie);//覆盖名为visitTimes的Cookie %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center" style="margin:10px;">
<fieldset>
<legend>登录信息</legend>
<form action="login.jsp" method="post">
<table>
<tr>
<td>你的账号:</td>
<td><%=username %></td>
</tr>
<tr>
<td>登陆次数:</td>
<td><%=visitTimes %></td>
</tr>
<tr>
<td></td>
<td>
<input type="button" value="刷新" onclick="location='<%= request.getRequestURI() %>?ts='+new Date().getTime();" class="button">
</td>
</tr>
</table>
</form>
</fieldset>
</div>
</body>
</html>
效果:
随便输入账号和密码,点击登录
Cookie与Session用法的更多相关文章
- Go语言之高级篇beego框架之cookie与session
1.cookie的用法 this.Ctx.SetCookie("name", name, maxage, "/") this.Ctx.SetCookie(&qu ...
- Django之会话机制cookie、session使用
login视图函数: def login(request): if request.method == 'POST': username = request.POST.get('username') ...
- 转载ASP.NET 状态管理Application,Session,Cookie和ViewState用法
转载原地址 http://www.cnblogs.com/cuishao1985/archive/2009/09/24/1573403.html ASP.NET状态管理 APPlication,Ses ...
- Cookie、Session登陆验证相关介绍和用法
一.Cookie和Session 首先.HTTP协议是无状态的:所谓的无状态是指每次的请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应直接影响,也不会直接 ...
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
浅谈JS中的!=.== .!==.===的用法和区别 var num = 1; var str = '1'; var test = 1; test == num //tr ...
- cookie和session简单的用法
一.登录成功则设置cookie和session 二.在登录页判断是否已记住密码 三.在首页判断,和创建会话区 四.在首页执行并显示
- Cookie、Session、localStorage、sessionStorage区别和用法
Cookie 在学习一个新知识点前,我们应该明白自己的学习目标,要带着疑问去学习,该小节须要了解 Cookies 什么是cookie,cookie的作用 cookie的工作机制,即cookie是运作流 ...
- PHP的学习--cookie和session
最近读了一点<PHP核心技术与最佳实践>,看了cookie和session,有所收获,结合之前的认识参考了几篇博客,总结一下-- 1. PHP的COOKIE cookie 是一种在远程浏览 ...
- Java——Cookie与Session
Cookie通过客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身份. 1.Cookie 1.1概念及使用方法 Cookie实际上是一小段文本信息.客户端请求服务器,如果服务 ...
随机推荐
- css优先级问题
关于CSS specificityCSS 的specificity 特性或称非凡性,它是衡量一个衡量CSS值优先级的一个标准,既然作为标准,就具有一套相关的判定规定及计算方式,specificity用 ...
- 把CentOS 7.x网卡名称eno16777736改为eth0
CentOS 7.x系统中网卡命名规则被重新定义,可能会是"eno167777xx"等,下面我们把网卡名称改为eth0这种. 一.cd /etc/sysconfig/networ ...
- [OC][转]UITableView属性及方法大全
Tip: UITableView属性及方法大全 (摘录地址) p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 1 ...
- 通过js引用外部脚本(嘿嘿,方便直接在浏览器上调试抓取代码)
最近折腾爬虫,后端使用jQuery进行数据采集,一般都是先从浏览器中将采集代码调试好后直接放到后端跑了. 有些网址没有引用jQuery,那调试起来就不方便了,可以用以下代码动态添加script标签,将 ...
- 纯硬盘安装Kali 无需U盘
工具: EasyBCD 安装后启动,然后依次点击 添加新条目 >> NeoGrub >> 安装 然后如下图 点击配置,然后弹出一个记事本窗口,先最小化.进行如下操作: 1.拷贝 ...
- Allegro 导入DXF文件,保留布好的线路信息
最近智能钥匙产品开发过程中,由于结构装配尺寸的偏差,需要对电路PCB外框OUTLINE进行缩小调整,并且USB插座定位孔改变. Allegro软件在线性绘制方面是有严重缺陷的,想绘制一个异形的板框比较 ...
- requirejs加载css样式表
1. 在 https://github.com/guybedford/require-css 下载到require-css包 2. 把css.js或者css.min.js复制到项目的js目录下 3. ...
- 手势(UIGestureRecognizer)
通过继承 UIGestureRecognizer 类,实现自定义手势(手势识别器类). PS:自定义手势时,需要 #import <UIKit/UIGestureRecognizerSubcla ...
- batch
%0 当前 batch 文件名 %n 第 n 个命令行参数(1 <= n <= 9) %* 所有的命令行参数 @echo off echo % echo % if not &q ...
- Codeforces 722D. Generating Sets
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...