weblogic和tomcat获取web路径的区别

首先在weblogic中部署时应该加上weblogic.xml文件
当我们把war工程放在tomcat服务器中使用如下代码可以获取到web应用目录

String path = request.getSession().getServletContext().getRealPath("/");

当我们用weblogic去部署的时候发现上面代码返回的值为null。

因此我们可以这样处理:

String path = request.getSession().getServletContext().getRealPath("/");
//如果没有获取到web应用目录,我们采用如下的方法
        if (path == null) {
            path = this.getClass().getClassLoader().getResource("/").getPath();
            path = path .substring(0, path .indexOf("WEB-INF"));
        }

获取我们直接用下面的代码获取web路径,这样tomcat和weblogic都适用
(不过大家习惯在tomcat中使用getServletContext().getRealPath获取路径)

String path = this.getClass().getClassLoader().getResource("/").getPath();
path = path .substring(0, path .indexOf("WEB-INF"));
发布了39 篇原创文章 · 获赞 157 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_34417749/article/details/81507767