最近写一个小程序,本来开发环境是JDK1.6,运行环境是Tomcat6.0,结果后来才知道需要部署在Linux平台Weblogic8.1中,可想而知面临着多大的改动,必经Weblogic8.1只支持JDK1.4,Servlet2.3。
到最后遇到一个棘手的问题,一个JavaBean中需要获取到项目的上下文路径,经过一番周折,试过一下几种方式
System.getProperty("user.dir"):/usr/local/bea/weblogic/user_projects/domains/base_domain new File(".").getAbsolutePath():/usr/local/bea/weblogic/user_projects/domains/base_domain/. new File("/"):/ this.getClass().getClassLoader().getResource("").getPath():/usr/local/bea/weblogic/user_projects/domains/base_domain/autodeploy/UpgradePro/WEB-INF/classes/ Thread.currentThread().getContextClassLoader().getResource(""):file:/usr/local/bea/weblogic/user_projects/domains/base_domain/autodeploy/UpgradePro/WEB-INF/classes/ FileHelperImpl.class.getClassLoader().getResource(""):file:/usr/local/bea/weblogic/user_projects/domains/base_domain/autodeploy/UpgradePro/WEB-INF/classes/ ClassLoader.getSystemResource(""):file:/usr/local/bea/weblogic/user_projects/domains/base_domain/ FileHelperImpl.class.getResource(""):file:/usr/local/bea/weblogic/user_projects/domains/base_domain/autodeploy/UpgradePro/WEB-INF/classes/com/mls/upgrade/biz/
结果都不能直接得到想要的结果,都需要手动拼接字符串,没办法只有通过Servlet监听context获取
public final class ContextHelper implements ServletContextListener { public static ServletContextEvent context = null; /** * ServletContext上下文 */ public void contextInitialized(ServletContextEvent context) { Factories.context = context; } public void contextDestroyed(ServletContextEvent arg0) { } }
在需要获取上下文路径的地址直接引用String contextPath=Factories.context.getServletContext().getRealPath("");即可达到目的。
结果问题又来了,项目部署时提示一个警告
[Application: '/rdsh/weblogic/bea/user_projects/domains/ s_srv/aaaa', Module: 'UpgradePro']: Deployment descriptor "w eb.xml" is malformed. Check against the DTD: org.xml.sax.SAX ParseException: cvc-elt.1: Cannot find the declaration of el ement 'web-app'. (line 5, column 52).>
原来web.xml中配置的
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
而WebLogic8.1只支持到2.3,于是改成
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
</web-app>
并将Listener提前到Servlet配置之前(否则会提示错误)即可。