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实际上是一小段文本信息.客户端请求服务器,如果服务 ...
随机推荐
- oracle的imp导入时覆盖目标数据库
背景:oracle正式库通过exp命令导出的dmp备份包,现在通过imp命令还原到测试库,测试库上面的表数据全部不要,要用新的. 方法:先删除用户.用户所在表空间,再新建用户和表空间,再imp导入. ...
- springboot中swaggerUI的使用
demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...
- Linux-设置固定IP
第一步:激活网卡 系统装好后默认的网卡是eth0,用下面的命令将这块网卡激活. # ifconfig eth0 up 第二步:设置网卡进入系统时启动 想要每次开机就可以自动获取IP地址上网,就要设置网 ...
- 此数据库文件与当前sql server实例不兼容
在vs2015导入mdf数据库文件时提示:此数据库文件与当前sql server实例不兼容. mdf文件的版本是SQL SERVER 2005的,而VS2015自带的数据库是LocalDB,直接导入该 ...
- gulp入门演练
一直想学习下gulp看了蛮多资料,然后总是感觉是是而非,突然开窍了,把自己学会的过程给大家分享下,入门超级简单的 gulp安装 安装gulp 如果参数-g 表示全局安装 $ npm install g ...
- $.inArray()
原文链接:http://www.css88.com/jqapi-1.9/jQuery.inArray/ jQuery.inArray( value, array [, fromIndex ] )返回: ...
- Android自定义View的三种实现方式
在毕设项目中多处用到自定义控件,一直打算总结一下自定义控件的实现方式,今天就来总结一下吧.在此之前学习了郭霖大神博客上面关于自定义View的几篇博文,感觉受益良多,本文中就参考了其中的一些内容. 总结 ...
- AWT编程学习01(未完成)
本文资料来源:<java疯狂讲义> 作者:李刚 终于要学习这一部分了~~虽然很多人(明明是绝大多数的人)说学这个没有用...而且有点过时了...但我觉得很有意思啊...感兴趣就学~~反正多 ...
- java中的数据结构(集合|容器)
对java中的数据结构做一个小小的个人总结,虽然还没有到研究透彻jdk源码的地步.首先.java中为何需要集合的出现?什么需求导致.我想对于面向对象来说,对象适用于描述任何事物,所以为了方便对于对象的 ...
- js计时器
js代码: <script language="javascript"> ,h=,s=,ss=; function second(){ )==){s+=;ss=;} & ...