Sometimes we encounter a big project. To facilitate some functions, we split the project into different independent web projects.
However, when we manage these projects, there is only one login port, and then the session is used in other projects to verify the identity.
1. Configure the conf/server. xml file in Tomcat. The configuration in
<Host name = "localhost" appbase = "webapps"
Unpackwars = "true" autodeploy = "true"
Xmlvalidation = "false" xmlnamespaceaware = "false">
<! -- AA and BB are two different web projects -->
<Context Path = "/AA" reloadable = "true" crosscontext = "true"/>
<Context Path = "/BB" reloadable = "true" crosscontext = "true"/>
</Host>
2. Store the session in the AA Project
Session. setattribute ("user", new date (). tostring () + "weiqingli ");
Servletcontext contexta = session. getservletcontext ();
Contexta. setattribute ("session", session );
3. Retrieve the session in the BB Project
Httpsession session1 = request. getsession ();
Servletcontext context = session1.getservletcontext ();
Servletcontext context1 = context. getcontext ("/AA ");
If (context1! = NULL &&! Context1.equals ("")){
Httpsession sess = (httpsession) context1.getattribute ("session ");
Out. println (sess. getattribute ("user "));
}
It may be easy to see that this is a one-way transfer session. In fact, to achieve two-way, the above program can be reversed and saved from BB, and AA is taken out.