Java操作Session与Cookie
1,Java操作Session
Java操作Session非常简单,步骤如下
1.1,在servlet中通过request获取session
HttpSession session = request.getSession(true);
true代表当前没有建立session则创建一个session,并返回这个session
false代表当前没有session,不做操作,返回null
默认为true
1.2,利用session对象设置属性或删除属性
添加
session.setAttribute("username", username);
session.setMaxInactiveInterval(60 * 30); //默认为秒
删除
session.removeAttribute("username")
2,Java操作Cookie
Java操作Cookie就有些需要注意的地方了
为此,建立一个操作cookie的工具类
CookieUtil.java
package org.guangsoft.util; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
*
* @author guanghe
*/
public class CookieUtil
{
/**
* 设置cookie
*
* @param response
* @param key cookie名字
* @param value cookie值
* @param maxAge cookie生命周期 以秒为单位
* @param path cookie传递路径
* @param domain cookie域
*/
public static void addCookie(HttpServletResponse response,
String key, String value, int maxAge, String path, String domain)
{
Cookie cookie = new Cookie(key, value);
cookie.setPath(path);
cookie.setDomain(domain);
if (maxAge > 0)
{
cookie.setMaxAge(maxAge);
}
response.addCookie(cookie);
} /**
* 根据名字获取cookie
*
* @param request
* @param name cookie名字
* @return
*/
public static Cookie getCookieByName(HttpServletRequest request, String name)
{
Cookie cookies[] = request.getCookies();
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
Cookie cookie = cookies[i];
if (name.equals(cookie.getName()))
{
return cookie;
}
}
}
return null;
}
}
调用代码:
UserService.java
public boolean setAutoLog(HttpServletRequest request,HttpServletResponse response, String username)
{
CookieUtil.addCookie(response, "username", username, 3600 * 24 * 3, "/manage/userServlet.action", "localhost");
return true;
} public boolean logout(HttpServletRequest request,HttpServletResponse response, String username)
{
CookieUtil.addCookie(response,"username","",0, "/manage/userServlet.action", "localhost");
return true;
}
严重提醒:删除Cookie时,只设置maxAge=0将不能够从浏览器中删除cookie,
* 因为一个Cookie应当属于一个path与domain,所以删除时,Cookie的这两个属性也必须设置。
* 误区:没有重视客户端发送到服务器端的cookie的path与domain值为空这个问题。
* 因为在登陆系统时,设置了Cookie的path与domain属性的值,就误认为每次客户端请求时,都会把Cookie的
* 这两个属性也提交到服务器端,但系统并没有把path与domain提交到服务器端(提交过来的只有Cookie的key,value值)。
Java操作Session与Cookie的更多相关文章
- C# 操作Session、Cookie,Url 编码解码工具类WebHelper
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text ...
- nodejs操作session和cookie
session: 安装模块 cnpm install express-session 引入session注册到路由 var express = require('express'); var sess ...
- 第7课:sql注入、操作session、cookie实例、网络编程、操作Excel
1. 简单讲一些sql注入的内容 name = 'zdq' sex = '女' cur.execute("select * from bt_stu where real_name='%s'& ...
- ecshop里操作session与cookie
目录 操作session 操作cookie html模板里提交保存用用户名 php里 js里保存cookie js里读取cookie html模板里smart的保留变量 html模板里取session ...
- yii 操作session和cookie
一,在Yii中使用session 1,CHttpSession 与原生态php5的session使用差别是,php5使用session_start();$_session['key'] = $valu ...
- Java服务端对Cookie的简单操作
Java服务端对Cookie的简单操作 时间 2016-04-07 10:39:44 极客头条 原文 http://www.cuiyongzhi.com/index.php/post/15.html ...
- JAVA操作COOKIE
JAVA操作COOKIE 1.设置Cookie Cookie cookie = new Cookie("key", "value"); cookie.setMa ...
- python运维开发(十九)----Django后台表单验证、session、cookie、model操作
内容目录: Django后台表单验证 CSRF加密传输 session.cookie model数据库操作 Django后台Form表单验证 Django中Form一般有2种功能: 1.用于做用户提交 ...
- ThinkPHP第二十六天(JQuery操作select,SESSION和COOKIE)
1.JQuery操作select,假设<select id="my"> A:双击选项<option>事件,应该是select的dbclick事件. B:获得 ...
随机推荐
- USACO 3.2 butter 最短路
堆优化dijkstra /* PROB:butter LANG:C++ */ #include <iostream> #include <cstdio> #include &l ...
- IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)
目录 . 引言 . IIS 6.0 FTP匿名登录.匿名可写加固 . IIS 7.0 FTP匿名登录.匿名可写加固 . IIS >= 7.5 FTP匿名登录.匿名可写加固 . IIS 6.0 A ...
- java反射学习
通过一个对象获得完整的包名和类名 package reflect; public class GetClass { public static void main(String[] args) { G ...
- java + jquery + ajax + json 交互
前端js部分: $.ajax({ async:true, cache:false, type:"POST", dataType : 'json', url:"/shopp ...
- Spring+Struts2+Mybatis框架搭建时的常见典型问题
搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired ...
- linux:Nginx+https双向验证(数字安全证书)
本文由邓亚运提供 Nginx+https双向验证 说明: 要想实现nginx的https,nginx必须启用http_ssl模块:在编译时加上--with-http_ssl_module参数就ok.另 ...
- 使用苏飞httphelper开发自动更新发布文章程序
最近新上线了一个网站,专门收集网上签到赚钱,有奖活动等等的网站 我就要集分宝 http://www.591jfb.com.新建立 了一个栏目"每日更新",这样就需要每天都登录到网站 ...
- OGNL和Struts2标签
OGNL和Struts2标签 你使用过的OGNL 页面获取并输出Action属性<s:property value="userName"/> 页面中获取request保 ...
- myBatis 实现用户表增操作(复杂型)
增加 @Test public void addTest(){ String resource = "mybatis-config.xml"; SqlSession sqlSess ...
- C# ManualResetEvent的使用
using System; using System.Threading; public class Example { // mre is used to block and release thr ...