欢迎访问binary的Blog   虚心使人进步,骄傲使人落后。

          W3CHINA Blog首页    管理页面    写新日志    退出



«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


登录

用户名称:
登陆密码:
密码保存:


联系我
email: binaryluo(at)gmail.com

我的分类

日志更新

最新评论

留言板

Blog信息

 
blog名称:二进制-虚心使人进步,骄傲使人落后。
日志总数:42
评论数量:370
留言数量:88
访问次数:638632
建立时间:2005年2月19日




[JAVA / J2EE]J2EE中的会话管理简介
原创空间,  随笔,  心得体会,  软件技术,  电脑与网络

binaryluo 发表于 2006/6/4 9:07:38

  在j2ee中会话管理有四种方法来保存当前的状态:    1.重写URL   2.隐藏域   3.Cookies   4.Session对象(推荐)    其中,前两种在HTML里也经常被用到。    重写URL,就是将要要传递到下一个页面的信息放在该页面URL的后面。   优点:简单,方便。   缺点:要传送的信息以明文方式放在URL后面,安全性低;而且一次传递的信息不能超过2000个字符。所以重写URL通常用于传输一些数据量较小,而且信息保密性要求不高的信息。使用格式如下:   url?name1=value1&name2=value2&…   例如,在index.jsp页面中有一个提交按钮,而search.jsp是用来处理该按钮要检索的关键字的,用重写URL实现如下: <input type="submit" name="Submit" value="搜索"><br/><a href="./search.jsp?key=计算机">高级搜索</a>    然后在search.jsp中用如下方式取得index.jsp传过来的信息: String key = request.getParameter(“key”)    在本例中,key的值是“计算机”。    隐藏域,顾名思义就是可以将要传递的信息隐藏以后传递给下一个页面,其实质就是将表单的method设置为post方式。   例如,在test.jsp中,写了一个隐藏域,名字叫“id”,值是“1”: <form method = post><input type = hidden name = id value = “1”>    要获得该隐藏域的信息仍然使用request的getParameter方法: String id = request.getParameter(“id”)    在本例中,id的值是“1”。    Cookies是将会话信息保存在客户端(浏览器)的方法,只要客户端不清除Cookies,它将永久保存。每个Cookies有一个名字和一个值。   创建一个cookies:   Cookie(String name, String value)    发送一个cookies到浏览器:   HttpServletResponse.addCookie(Cookie aCookie)   取cookies的信息: HttpServletRequest.getCookies()   示例: Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = request.getParameter(“username”); String password = request.getParameter(“password”);  If (login(userName, password)) {  Cookie c1 = new Cookie(“username”, username);  Cookie c2 = new Cookie(“password”, password);  Response.addCookie(c1);  Response.addCookie(c2);  Response.setContentType(“test/html”);}}    Session对象,应该说是会话管理中最简单,而且是最强大的一种方法。Session对象使用的是HttpSession接口,该接口中有众多可使用的方法,可以去查j2sdk文档,在这里不再赘述。   HttpSession的数据存储方式是:   <”key”, object>   设置session对象的参数: void setAttribute(String,Object)    获取session对象的参数: Object getAttribute(String)    示例:   Content1Servlet: Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); Session.setAttribute(“loggedIn”, new String(“true”)); Response.sendRedirect(“Content2Servlet”); ……}    Content2Servlet: Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); if (session == null)  Response.sendRedirect(loginUrl); else {  String loggedin = (String)session.getAttribute(“loggedIn”);  if (!loggedIn.equals(“true”))   response.sendRedirect(loginUrl); } ……}   通常,保存对象(Object)一般都用Session对象。 


阅读全文(3594) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.186 second(s), page refreshed 144773317 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号