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实际上是一小段文本信息.客户端请求服务器,如果服务 ...
随机推荐
- 通知Notification
步骤: 1.调用getSystemService()获取NotificationManager:NotificationManager manager = (NotificationManager)g ...
- sql-GOTO跳转
--声明变量 DECLARE @X INT --标记GOTO跳转位置 TEST: PRINT @X --WHILE @X<=3 --GOTO跳转到执行位置 GOTO TEST
- 6.Swift协议|扩展|访问权限|异常调试|类型转换|运算函数|ARC|类类型初试化器|值类型初始化器
1. 协议(Protocol):与OC之间唯一不同的是Swift中的协议不管是属性还时方法全部是必须实现的 /** protocol*/ protocol FullNamed { /** 计算属性申明 ...
- MS SQL-Server快捷键
快捷键 功能 Ctrl+Shift+B 生成解决方案 Ctrl+F7 生成编译 Ctrl+O ...
- 使用spring aspect控制自定义注解
自定义注解:这里是一个处理异常的注解,当调用方法发生异常时,返回异常信息 /** * ErrorCode: * * @author yangzhenlong * @since 2016/7/21 */ ...
- OpenLayers图形与列表互动
项目上遇到这样一种需求:查询数据库后得到结果(带地理位置的)列表,每个结果在地图上都是一个四边形,四边形之间有交叉,有重叠,需要实现地图上的四边形和结果列表的互动.抛开其他逻辑功能,互动需求可以表示为 ...
- AnguarJS测试的实施步骤整理
最近开发用到了AngularJS,据说目前大型系统都用这个作为前端.最近参与的一个项目,web部分重度使用了AngularJS,整个前端架构有组织有纪律.所谓的有纪律就是说,有比较完善的测试用例,用上 ...
- md语法之行内代码和代码片
md语法之行内代码和代码片 比如说要在行内写上一句或者半句代码(代码的意思就是某种脚本语言), 用撇号围起来就可以了. 比如: import pandas as pd 写代码片(单独的一块脚本语言)的 ...
- error: command 'cc' failed with exit status 1
报错: Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/ ...
- 转:logBack.xml配置路径
http://blog.csdn.net/z69183787/article/details/30284391 http://www.cppblog.com/fwxjj/archive/2012/08 ...