一个tomcat下两个项目(ZK项目)共享session

本文探讨了在使用servletContext(application级别)时,多用户登录信息被覆盖的问题,并提供了一种解决方案:采用单点登录(CAS认证),同时展示了如何通过配置实现跨应用的session共享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下内容多个用户登录时,后一个用户的信息会将前一个用户的信息冲掉,因为servletContext是application级别,希望看的本篇博客的人注意,解决方案还未完全完成,过两天再贴。


最后用的是单点登录,CAS认证,网上已有的比较成熟的解决方案。


发送项目为项目C

public void onSetTextbox(){
		Textbox texb = (Textbox)this.getFellow("texb");
		SimpleSession s = (SimpleSession) Executions.getCurrent().getDesktop().getSession();
		HttpSession session = (HttpSession) s.getNativeSession();
		session.setAttribute("username", texb.getValue());
		session.setAttribute("password", "qwert");
		session.setMaxInactiveInterval(6565);
		ServletContext ContextA = session.getServletContext();
		ContextA.setAttribute("session", session);
		//测试
		HttpSession session2 =(HttpSession)ContextA.getAttribute("session");
		System.out.println("IN SessionRangleServlet name : "+session2.getAttribute("username"));
		System.out.println("IN SessionRangleServlet name : "+session2.getAttribute("password"));
	}
接受项目为D

public void onGetTextbox() throws InterruptedException{
		
	    SimpleSession s = (SimpleSession) Executions.getCurrent().getDesktop().getSession();
	    HttpSession session1 = (HttpSession) s.getNativeSession();
	    ServletContext Context = session1.getServletContext();  
	    ServletContext ContextA= Context.getContext("/C");    
	       
	    HttpSession session2 =(HttpSession)ContextA.getAttribute("session");
		 
		String message = "base传过来的user为:"+session2.getAttribute("username")+"&&&"+session2.getAttribute("password");
		System.out.println(message);
		Messagebox.show(message);
	}
tomcat配置crossContext="true"

<Context docBase="C" path="/C" reloadable="true"  crossContext="true"  source="org.eclipse.jst.jee.server:C"/>
      <Context docBase="D" path="/D" reloadable="true" crossContext="true"  source="org.eclipse.jst.jee.server:D"/>


如果是普通的java项目session获得

HttpSession session1 =request.getSession(); 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值