Tomcat共享ServletContext

1、介绍

一个Tomcat下多war之间共享Session对象,可以通过先共享ServletContxt对象然后再从ServletContext中获取Session对象。并讲述开发过程中共享ServletContext的Intellij IDEA的配置操作。

注: 本文讲述的是一个Tomcat共享Session对象,并非分布式应用多Tomcat之间共享Session对象的案例.

2、思路详解

Tomcat共享ServletContxt只需在 conf/server.xml中添加crossContext=”true”属性配置即可完成,为了方便理解请看以下配置案例

2.1 项目部署

比方tomcat/webapps下有一个test-web工程,如图所示
Tomcat共享ServletContext_第1张图片

2.2 非共享

找到server.xml中标签,非共享ServletContxt情况下的配置如下所示

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

    <Context path="/test-web" docBase="tomcat/webapps/test-web" debug="0" reloadable="true"  />

Host>

Context中属性详解
path: 表示上下文根,比方http://localhost:8080/test-web。path的value值就是test-web
docBase: 表示部署到tomcat/webapps目录下web工程路径。:一般我会写绝对路径,实验过程中请您修改为自己的路径地址

2.3 共享

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

    <Context path="/test-web" docBase="tomcat/webapps/test-web" debug="0" reloadable="true" crossContext="true" />

Host>

Context中属性详解
crossContext: 就是在配置tomcat下多war之间共享ServletContext对象。

3、Intellij IDEA的配置

3.1 介绍

用Intellij IDEA开发Java代码过程中的配置,需要基于以上tomcat/conf/server.xml文件,所以开发时事先在server.xml中的Host标签中写好Context配置,假定此时向tomcat中发布两个war包:webA和webB如图所示
Tomcat共享ServletContext_第2张图片

3.2 修改发布路径

将Intellij IDEA编译后的工程发布到tomcat/webapps目录下,所以需要修改Intellij IDEA的Output directory属性。请打开Project Structure -> Aritifacts,操作界面如下
Tomcat共享ServletContext_第3张图片

此处举例说明将test-web工程发布到tomcat/webapps目录下

步骤详解:
1. test-web:war exploded,此选项为热部署工程
2. Output directory: 修改为输出到tomcat/webapps目录下

4、代码验证

4.1 介绍

为了验证Tomcat共享ServletContext功能是否时限,通过测试代码进行验证,此时您需要创建两个web工程,例如一个叫webA、另一个叫webB

4.2 webA工程代码

/**
 * @Description Tomcat下多war之间ServletContext共享(或Session共享)
 * @author wangjp
 * @Date: 2017-12-11 18:24
 */
@Controller
@RequestMapping("/shareSessionController")
public class ShareSessionController {

    private static final Logger logger = LoggerFactory.getLogger(ShareSessionController.class);

    /**
     * @Description 分享Session对象,供同一个Tomcat下的其他war使用Session对象
     * @author wangjp
     * @Date: 2017-12-11 19:07
     */
    @ResponseBody
    @RequestMapping(value = "/shareSession", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void shareSession(HttpServletRequest request){
        try {
            //获取Session对象
            HttpSession session = request.getSession();

            //设置一个测试value值
            session.setAttribute("webASessionKey", "Hero-Web中Session中的值");

            //获取ServletContext对象
            ServletContext servletContext =session.getServletContext();

            //将Session对象放入ServletContext对象中
            servletContext.setAttribute("session", request.getSession());

            logger.debug("执行结束");
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

4.3 webB工程代码

/**
 * @Description Tomcat下多war之间ServletContext共享(或Session共享)
 * @author wangjp
 * @Date: 2017-12-11 18:24
 */
@Controller
@RequestMapping("/shareSessionController")
public class ShareSessionController {

    private static final Logger logger = LoggerFactory.getLogger(ShareSessionController.class);

    /**
     * @Description 分享Session对象,供同一个Tomcat下的其他war使用Session对象
     * @author wangjp
     * @Date: 2017-12-11 19:07
     */
    @ResponseBody
    @RequestMapping(value = "/shareSession", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void shareSession(HttpServletRequest request){
        try {
            //获取ServletContext对象
            ServletContext servletContext= request.getSession().getServletContext()
                    .getContext("/webA");

            //获取Web A的Session对象
            HttpSession webAsession = (HttpSession) servletContext.getAttribute("session");

            //获取Web A的Session对象中的值
            Object webASessionValue = webAsession.getAttribute("webASessionKey");

            logger.debug("获取Web A的Session对象中webASessionKey的值: {}", webASessionValue);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

4.4 验证操作

先访问webA工程的ShareSessionController,再访问webB工程的ShareSessionController

Hero鹏,一个伟大的名字

如有疑问或是吐槽者,都可以联系我,

HomePage: www.crazyboy2016.com
Email: [email protected]
GitHub: wangjianpeng2016
Blog: Hero鹏

你可能感兴趣的:(Tomcat共享ServletContext)