java中Cookie被禁用后Session追踪问题
来源:才华咖 本文已影响1.59W人
来源:才华咖 本文已影响1.59W人
这篇文章主要介绍了Java中Cookie被禁用后Session追踪问题,非常不错,具有参考借鉴价值,需要的朋友可以参考下
一.服务器端获取Session对象依赖于客户端携带的Cookie中的JSESSIONID数据。如果用户把浏览器的隐私级别调到最高,这时浏览器是不会接受Cookie、这样导致永远在服务器端都拿不到的JSESSIONID信息。这样就导致服务器端的Session使用不了。
Java针对Cookie禁用,给出了解决方案,依然可以保证JSESSIONID的传输。
Java中给出了再所有的路径的后面拼接JSESSIONID信息。
在 Session1Servlet中,使用deURL(url) 对超链接路径拼接 session的唯一标识
// 当点击 的.时候跳转到 session2
ontentType("text/html;charset=utf-8");
//此方法会在路径后面自动拼接sessionId
String path = deURL("/day11/session2");
tln(path);
//页面输出
riter()tln("ip地址保存成功,想看 请<a href='" + path + "'>点击</a>");
二.在response对象中的提供的encodeURL方法它只能对页面上的超链接或者是form表单中的action中的路径进行重写(拼接JSESSIONID)。
如果我们使用的重定向技术,这时必须使用下面方法完成:其实就是在路径后面拼接了 Session的唯一标识 JSESSIONID。
// 重定向到session2
String path = deRedirectURL("/day11/session2");
tln("重定向编码后的路径:" + path);
Redirect(path);
session2代码,获得session1传过来的ID
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 需求:从session容器中取出ip
// 获得session对象
HttpSession session = ession();
// 获取ip地址
String ip = (String) ttribute("ip");
// 将ip打印到浏览器中
ontentType("text/html;charset=utf-8");
riter()tln("IP:" + ip);
}
session1代码
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 需求:将ip保存到session中
// 获取session
HttpSession session = ession();
// 获得ip
String ip = emoteAddr();
// 将ip保存到session中
ttribute("ip", ip);
// 需求2:手动的将 session对应的cookie持久化,关闭浏览器再次访问session中的数据依然存在
// 创建cookie
Cookie cookie = new Cookie("JSESSIONID", d());
// 设置cookie的最大生存时间
axAge(60 * 30);
// 设置有效路径
ath("/");
// 发送cookie
ookie(cookie);
// 当点击 的时候跳转到 session2
// ontentType("text/html;charset=utf-8");
// String path = deURL("/day11/session2");
// tln(path);
// riter()tln("ip地址保存成功,想看 请<a href='" + path + "'>点击</a>");
// 重定向到session2
String path = deRedirectURL("/day11/session2");
tln("重定向编码后的路径:" + path);
Redirect(path);
}
以上所述是小编给大家介绍的java中Cookie被禁用后Session追踪问题,希望对大家有所帮助!
Fancy the original or movie ones大学英语作文
托福英语写作范文:Examinations exert a pernicious influence on educat
关于javascript中cookie对象用法的实例分析
英语作文Never judge a book by its cover
英语阅读:Life is inevitable consequence of physics
六年级英语上册Unit2 New York is in the east of America说课稿
初一英语Unit 12 My favorite subject is science 课后练习题答
The Attractions of Video Games大学英语作文
Myfavoritesubjectisscience教案
Unit 2 I think that mooncakes are delicious说课稿范文
大学英语演讲稿:Television News Coverage
Unit six Opening and Closing Ceremonies口译材料
JavaScript中Cookie操作实际案例
英语阅读:African Union Leaders Talk Mali, Chinese Investmen
英语阅读:VIT declines to acquire Ang Mo Kio property
Java面试中最常问的问题和答案
英语Lesson8家庭作业:Dannys favourite colour
英语作文:大学生村官 College Graduates Work as Village Offic
雅思阅读模拟试题:Sun's fickle heart may leave us cold
At Climate Summit, Hope for Emissions Deal美文欣赏
A Letter to Univsity Pesident about the Canteen Service on C
I Love Four Seasons英语作文
Should Private Cars Be Encouraged in China高中英语作文
分享SAT阅读练习题:Reading Comprehension Test 14
《特别追踪》观后感
英语新闻阅读:2017's bestselling books in China
英语作文Some advice on how to learn English well
关于javascript中encodeURI和decodeURI的使用方法
Modern Technology Makes Life More Conv英语六级作文
The value of reading books英语作文
“Does Pressure Equal Motivation?”高中英语作文范文
Food and place of interest in Beijing初中英语作文
Movies and Books英语作文
php中session和cookie的区别
追踪小绿人读后感
关于JavaScript学习笔记之Cookie对象
英语阅读:Are You Tricked by These Optical Illusions
英语满分作文:Reading-TheBestMeanstoAcquireknowledge
英语作文:The Value of Reading Books
JavaScript instanceof 的使用方法有哪些