同一个tomcat下两个项目前后端信息的共享


文章目录


前言

这相当于是仿照之前的大神写的,唯一注意的几个点就是server.xml里面内容一定要修改正确,session可以共享信息,但如果需要得到每个后台登陆者的信息时,需要用到sessionID和cookie的结合。


一、修改tomcatconf底下server.xml文件

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
			xmlValidation="false" xmlNamespaceAware="false">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
		 <Context path="/项目A"  reloadable="true" crossContext="true" debug="9"/> 
		<Context path="/项目B"  reloadable="true" crossContext="true" debug="9"/>
      </Host>

二、将项目A的信息传到项目B

                    session.setAttribute("user",sessionMap); 
					ServletContext ContextA =session.getServletContext(); 
					ContextA.setAttribute("session", session);

 其中sessionMap相当于是需要传递的信息,可以是类型,也可以是集合,但不能是实体层对象,因为项目A接受的时候辨别不出来

2.项目B接收项目A的信息

            HttpSession session = request.getSession();
			ServletContext Context = session.getServletContext();
			ServletContext Context1 = Context.getContext("/项目B");
            if (Context1 != null && !Context1.equals("")) {
				HttpSession sess = (HttpSession) Context1.getAttribute("session");
				if (sess != null && sess.getAttribute("user") != null) {
					HashMap<String, HashMap> sessionId = (HashMap<String, HashMap>)         
             sess.getAttribute("user");
            }

 因为我发送的值是hashMap,所以用HashMa接收。


总结

例如:以上就是今天要讲的内容,本文仅仅简单介绍了session的使用,如果需要得到每个用户的信息时,session共享是不行的,需要用到cookie和sessionid。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值