1、 JDK和Servlet版本问题
WebLogic 8.1 sp4以前(包括sp4)只支持JDK1.4,建议使用JDK1.4进行编译代码,有时JDK1.5编译的程序无法运行,由于WebLogic 8.1不支持J2EE1.4,不要使用Servlet2.4和JSP2.0进行编码。
比如:在tomcat下代码里中可以有response.setCharacterEncoding(“UTF-8″)方法,这是servlet2.4的方法,所以要发布到weblogic上需要把此方法注释掉。
另外关于查看weblogic支持的servlet版本方法如下:
Java代码
javax.servlet.ServletContext
getMajorVersion() //主版本号(2)
getMinorVersion() //次版本号 (3)
javax.servlet.ServletContext
getMajorVersion() //主版本号(2)
getMinorVersion() //次版本号 (3)
结果分别为2、3的话就说明,版本号为 servlet2.3
2、 web.xml 中的启动加载问题
在TOMCAT中,加载Struts的顺序是通过servlet加载,排在Listener加载之后。如果在Struts中使用Plugin,会在TOMCAT启动的最后加载,所以在Plugin中可以使用Spring中的Bean。
移植到WebLogic后,Struts会在容器启动的时候全部加载,包括Plugin。这样就出现了在Plugin加载的时候,不能得到 Spring管理的Bean,也就是说Struts Plugin在WebLogic里不能使用Spring管理的Bean。所以如果需要启动时加载部分代码,建议使用Servlet init()方法。
Spring为通过Web启动的程序提供了一个工具,该工具可以从Context中直接得到WebApplicationContext,其工具的方法签名如下:
Java代码
org.springframework.web.context.support.WebApplicationContextUtils. getWebApplicationContext(ServletContext);
org.springframework.web.context.support.WebApplicationContextUtils. getWebApplicationContext(ServletContext);
org.springframework.web.context.support.WebApplicationContextUtils. getWebApplicationContext(ServletContext);
org.springframework.web.context.support.WebApplicationContextUtils. getWebApplicationContext(ServletContext);
3、 JSP 的 Include问题 (建议使用动态引入)
在BEA WebLogic中不允许在一个文件中出现一次以上类似<%@ page contentType=”text/html; charset=GBK”%>的代码,所以使用include file时,请将被include的文件中类似代码删除。
在TOMCAT时允许上述代码出现多回,并且使用include file时,被include的文件中,不包含上述代码,编译后客户端显示为乱码。BEA为此解释为TOMCAT不符合J2EE规范。
为了增加代码的通用性和可移植性,建议使用<jsp:include>方式。
<jsp:include>将被include的jsp代码视为独立存在的文件,所以可以在不同文件内使用多个<%@ page contentType=”text/html; charset=GBK”%>。<jsp:include>直接传参由<jsp:param>标签完成,在被 include页面可以通过request得到传入的值,也可以通过request.setAttribute()、 request.getAttribute()进行内外文件参数传递。
4、 对Log4j支持问题
打包成.war部署到WebLogic后,出现如下问题:
Java代码
Error: weblogic.management.DeploymentException: Cannot set web app root system property when WAR file is not expanded – with nested exception:
[java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded]
Error: weblogic.management.DeploymentException: Cannot set web app root system property when WAR file is not expanded – with nested exception:
[java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded]
Error: weblogic.management.DeploymentException: Cannot set web app root system property when WAR file is not expanded – with nested exception:
[java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded]
Error: weblogic.management.DeploymentException: Cannot set web app root system property when WAR file is not expanded – with nested exception:
[java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded]
问题解决:通常您不需要亲自编写servlet或者listener,比如直接利用log4j的 com.apache.jakarta.log4j.Log4jInit类,Spring的 org.springframework.web.util.Log4jConfigServlet和 org.springframework.web.util.ServletContextListener方式配置,找到.Log4jConfigServlet和ServletContextListener的源码,他们都在适当的地方(callback method)调用了Log4jWebConfigurer.initLogging(getServletContext());定位到这个方法,第一句就是:WebUtils.setWebAppRootSystemProperty(servletContext);再定位到该方法,方法很短:
Java代码
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
String oldValue = System.getProperty(key);
if (oldValue != null) {
throw new IllegalStateException(“WARNING: Web app root system property already set: ” + key + ” = ” + oldValue + ” – Choose unique webAppRootKey values in your web.xml files!”);
}
String root = servletContext.getRealPath(“/”);
if (root == null) {
throw new IllegalStateException(“Cannot set web app root system property when WAR file is not expanded”);
}
System.setProperty(key, root);
servletContext.log(“Set web app root system property: ” + key + ” = ” + root);
}
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
String oldValue = System.getProperty(key);
if (oldValue != null) {
throw new IllegalStateException(“WARNING: Web app root system property already set: ” + key + ” = ” + oldValue + ” – Choose unique webAppRootKey values in your web.xml files!”);
}
String root = servletContext.getRealPath(“/”);
if (root == null) {
throw new IllegalStateException(“Cannot set web app root system property when WAR file is not expanded”);
}
System.setProperty(key, root);
servletContext.log(“Set web app root system property: ” + key + ” = ” + root);
}
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
String oldValue = System.getProperty(key);
if (oldValue != null) {
throw new IllegalStateException(“WARNING: Web app root system property already set: ” + key + ” = ” + oldValue + ” – Choose unique webAppRootKey values in your web.xml files!”);
}
String root = servletContext.getRealPath(“/”);
if (root == null) {
throw new IllegalStateException(“Cannot set web app root system property when WAR file is not expanded”);
}
System.setProperty(key, root);
servletContext.log(“Set web app root system property: ” + key + ” = ” + root);
}
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
String oldValue = System.getProperty(key);
if (oldValue != null) {
throw new IllegalStateException(“WARNING: Web app root system property already set: ” + key + ” = ” + oldValue + ” – Choose unique webAppRootKey values in your web.xml files!”);
}
String root = servletContext.getRealPath(“/”);
if (root == null) {
throw new IllegalStateException(“Cannot set web app root system property when WAR file is not expanded”);
}
System.setProperty(key, root);
servletContext.log(“Set web app root system property: ” + key + ” = ” + root);
}
系统需要读取webAppRootKey这个参数,所以在部署到WebLogic里的时候,在web.xml中手动添加如下代码:
Xml代码
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
WebLogic自身也包含对Log4j的支持,在打包部署(.war)的时候,会和Spring的 org.springframework.web.util.Log4jConfigListener有冲突(拷贝到WebLogic散放部署不会出错)。所以改用Servlet加载。(不通过应用加载Log4j好像也可以使用,但未进行完整测试,下面代码修改后,系统会报Log4j加载重复错误,不影响应用启动。)
web.xml中删除下面代码:
Xml代码
<listener id=”log4jConfigListener”>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener id=”log4jConfigListener”>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener id=”log4jConfigListener”>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener id=”log4jConfigListener”>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
将Listener加载改为通过Servlet加载,再在web.xml增加:
Xml代码
<servlet>
<servlet-name>log4jConfigListener</servlet-name>
<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>log4jConfigListener</servlet-name>
<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>log4jConfigListener</servlet-name>
<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>log4jConfigListener</servlet-name>
<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>