同一浏览器中同一JavaWeb程序不共享session方法

版权声明:本文为博主原创文章,未经博主容许不得转载。 https://blog.csdn.net/b2084005/article/details/30222735
一、要求html

   在使用struts1开发JavaWeb项目中,要求超级管理员登陆进入以后,展现普通用户列表,在普通用户列表中添加管理用户按钮,点击后,在浏览器中另外弹出一个标签页,并且该标签页是选中的普通用户登陆后的界面。java

二、产生问题浏览器

a、点击普通用户登陆后,session中的用户信息已经变为了该用户的信息了,若是再次刷新超级管理员登陆的界面,此时,界面就变为选中的普通用户登陆的界面了。服务器

b、同一个系统,在同一浏览器中的session的ID是相同的,此sessionID是服务器建立session产生的,没法在客户端修改sessionid,若想修改sessionid,得从服务器端修改(没试过)。session

三、解决办法app

   a、生成一个不重复的值,我取名为siddom

    例、Random random = new Random();  jsp

StringBuffer sid = new StringBuffer();  ui

sid = sid.append(System.currentTimeMillis()); //取系统时间  .net

//  加上10为0-9随机数确保sid不重复  

for (int i = 0; i < 10; i++) {  

    sid = sid.append(random.nextInt(10));  

}

   b、把须要的用户相关的字段和sessionid一块儿写到一个Map中

例、Map sessionMap = new HashMap();  

sessionMap.put("username", username); 

sessionMap.put("userpwd", userpwd);

sessionMap.put("sessionid", session.getId());

sessionMap.put("usertype", userbean.getUsertype());

 

   c、把sid和map关联起来添加到session中

例、session.setAttribute(sid.toString(), sessionMap); 

   d、把sid传递给jsp页面(需经过sid获取用户信息)

例、ActionRedirect redirect = new ActionRedirect(actionMapping

.findForward("index"));

redirect.addParameter("sid",sid.toString());  request.getSession().setAttribute("userbean",userbean);

return redirect;

e、jsp页面经过sid获取用户信息

例、String sid = request.getParameter("sid");  

Map utitle = new HashMap();

utitle = (Map)request.getSession().getAttribute(sid); 

String username = (String)utitle.get("username");

String usertype = (String)utitle.get("usertype");

String uid = (String)utitle.get("uid");

System.out.println(uid+"=====uid===~~~====");

f、把获取的值传递给须要的页面

  例、 <iframe scrolling="auto" frameborder="0" height="100%"  width="100%"src="<%=appContext  %>/main/main.jsp?usertype=<%=usertype %>&uid=<%=uid %>"    name="mainFrame"></iframe>

g、jsp页面获取须要的值

例、String  usertype1 = "";

String  uid = "";

usertype1 = (String)request.getParameter("usertype");

uid = (String)request.getParameter("uid");

参考文章:http://vearn.iteye.com/blog/376407 、

http://www.blogjava.net/DreamAngel/archive/2012/06/08/380306.html和

相关文章
相关标签/搜索