以下是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用法的更多相关文章

  1. Go语言之高级篇beego框架之cookie与session

    1.cookie的用法 this.Ctx.SetCookie("name", name, maxage, "/") this.Ctx.SetCookie(&qu ...

  2. Django之会话机制cookie、session使用

    login视图函数: def login(request): if request.method == 'POST': username = request.POST.get('username') ...

  3. 转载ASP.NET 状态管理Application,Session,Cookie和ViewState用法

    转载原地址 http://www.cnblogs.com/cuishao1985/archive/2009/09/24/1573403.html ASP.NET状态管理 APPlication,Ses ...

  4. Cookie、Session登陆验证相关介绍和用法

    一.Cookie和Session 首先.HTTP协议是无状态的:所谓的无状态是指每次的请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应直接影响,也不会直接 ...

  5. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  6. cookie和session简单的用法

    一.登录成功则设置cookie和session 二.在登录页判断是否已记住密码 三.在首页判断,和创建会话区 四.在首页执行并显示

  7. Cookie、Session、localStorage、sessionStorage区别和用法

    Cookie 在学习一个新知识点前,我们应该明白自己的学习目标,要带着疑问去学习,该小节须要了解 Cookies 什么是cookie,cookie的作用 cookie的工作机制,即cookie是运作流 ...

  8. PHP的学习--cookie和session

    最近读了一点<PHP核心技术与最佳实践>,看了cookie和session,有所收获,结合之前的认识参考了几篇博客,总结一下-- 1. PHP的COOKIE cookie 是一种在远程浏览 ...

  9. Java——Cookie与Session

    Cookie通过客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身份. 1.Cookie  1.1概念及使用方法 Cookie实际上是一小段文本信息.客户端请求服务器,如果服务 ...

随机推荐

  1. ubuntu一些常用的命令

    1.docker里的ubuntu不知道密码,更新密码 sudo passwd 2.解压zip文件 unzip xx.zip 3.安装LAMP (1)sudo apt-get install apach ...

  2. python之前端

    一.Html总结 1.Html简介 HTML 是一种标记语言 忽略大小写,语法宽松 使用 HTML 标记和元素,可以: 控制页面和内容的外观 发布联机文档 使用 HTML 文档中插入的链接检索联机信息 ...

  3. Xcode6 ADD Copy Files Build Phase 是灰色的

    在学习的怎样写frameWork的时候,查看一个教程How to Create a Framework for iOS  [一个中文翻译 创建自己的framework] 其中一个步骤就是添加一个Cop ...

  4. 3d游戏模型及地形提取及导航

    支持所有DirectX的游戏模型提取 有需要的可以直接联系我!QQ290387340

  5. 怎样设置域名带www和不带www都可以访问

    1,域名解析添加两条A记录 2,IIS域名绑定添加两个主机头

  6. centos7 dokcer fastdfs

    docker run --name=fastdfstmp -tid centos /bin/bash docker cp /home/fastdfs fastdfstmp:/home docker e ...

  7. php+js进度读取条

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. python 01

    注意Python 是大小写敏感的,即print 与Print 不一样 推荐编辑器 vim & sublime 如何运行 #!/usr/bin/python#Filename: hellowor ...

  9. javascrpt事件

    1.HTML事件处理程序:就是事件直接写在HTML文档中,其特点就是HTML和Js紧密的结合在一起,缺点就是修改不方便,需要改动js和HTML两处.比如: <button onclick=&qu ...

  10. offsetwidth/clientwidth的区别

    clientWidth是对象看到的宽度(不含边线,即border)scrollWidth是对象实际内容的宽度(若无padding,那就是边框之间距离,如有padding,就是左padding和右pad ...