前言
这相当于是仿照之前的大神写的,唯一注意的几个点就是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 "%r" %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。