Tomcat session sharing: session sharing between different tomcat projects and tomcatsession
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.
View tomcat's explanation about the emptySessionPath in HTTP ctor as follows:
If set to true, all paths for session cookies will be set /. this can be useful for portlet specification implementations. if not specified, this attribute is set to false. A side effect to setting this to true, is that if Tomcat creates a new session it will attempt to use the cookie session id if supplied by the client.
Therefore, we need to share data between two WEB pages in tomcat through sessions.
Each WEB application has a unique ServletContext instance object, and all the servlets under it share this ServletContext.
Use the setAttribute () method in ServletContext to pass the Session and then get the session instance in another WEB program.
1: Modify the Tomcat --- conf ---- server. xml file
Xml Code
<Host appBase = "webapps" autoDeploy = "true" name = "localhost" unpackWARs = "true" xmlNamespaceAware = "false" x mlValidation = "false"> </Host>
To:
<Host appBase = "webapps" autoDeploy = "true" name = "localhost" unpackWARs = "true" xmlNamespaceAware = "false" x mlValidation = "false">
<Context path = "/project A" reloadable = "false" crossContext = "true"> </Context>
<Context path = "/Project B" reloadable = "false" crossContext = "true"> </Context>
</Host>
<Host appBase = "webapps" autoDeploy = "true" name = "localhost" unpackWARs = "true" xmlNamespaceAware = "false" x mlValidation = "false"> </Host>: <Host appBase = "webapps" autoDeploy = "true" name = "localhost" unpackWARs = "true" xmlNamespaceAware = "false" x mlValidation = "false"> <Context path = "/ project A "reloadable =" false "crossContext =" true "> </Context> <Context path ="/Project B "reloadable =" false "crossContext =" true "> </ context> </Host>
Note that the crossContext attribute is intended in the help document.
CrossContext: Set to true if you want callwithin this application to ServletContext. getContext () to successfully return a request dispatcher for other web applications running on this virtual host. set to false (the default) in security conscious environments, to make getContext () always return null.
Setting true indicates that you can call another WEB application to obtain the ServletContext through ServletContext. getContext () and then call its getattribute () to obtain your desired object.
2: Write the following code in Project:
We assume that
Project A is/myweb
Project B is/w2
// The following content is used to test the sharing session between different projects under the same tomcat.
Java code
HttpSession session = req. getSession ();
Session. setAttribute ("name", "xbkaishui ");
Session. setMaxInactiveInterval (6565 );
ServletContext ContextA = req. getSession (). getServletContext ();
ContextA. setAttribute ("session", req. getSession ());
HttpSession session = req.getSession(); session.setAttribute("name", "xbkaishui"); session.setMaxInactiveInterval(6565); ServletContext ContextA =req.getSession().getServletContext(); ContextA.setAttribute("session", req.getSession());
// Test
Java code
Out. println ("IN SessionRangleServlet name:" + session. getAttribute ("name "));
out.println("IN SessionRangleServlet name : "+session.getAttribute("name"));
3. In Project B, write the following code to retrieve the Session
Java code
HttpSession session1 = req. getSession ();
ServletContext Context = session1.getServletContext ();
// The Virtual Path of Project a is passed here
ServletContext Context1 = Context. getContext ("/myweb ");
System. out. println (Context1 );
HttpSession session2 = (HttpSession) Context1.getAttribute ("session ");
System. out. println ("the user passed by the base is:" + session2.getAttribute ("name "));
HttpSession session1 = req. getSession (); ServletContext Context = session1.getServletContext (); // The Virtual Path of Project a is ServletContext Context1 = Context. getContext ("/myweb"); System. out. println (Context1); HttpSession session2 = (HttpSession) Context1.getAttribute ("session"); System. out. println ("the user passed by the base is:" + session2.getAttribute ("name "));
Then redeploy.