Java web应用中如何判断Web容器类型

来源:互联网 发布:科力达传输软件 编辑:程序博客网 时间:2024/06/05 20:19

问题背景:最近在开发一个人行的项目,由于不同的商行所使用的WEB容器不一样,导致同样的代码在不同的容器中运行的效果不一样。因此想在代码中添加容器判断,从而让应用自动选择不同的实现。

问题描述:开发阶段使用的是jetty6,商行使用的应用服务器有tomcat7、websphere application server8.5.5,JDK版本是1.6

解决方法:

1)在项目中引入portal-kernel.jar,pom.xml中添加如下代码:

<!-- https://mvnrepository.com/artifact/com.liferay.portal/portal-kernel --><dependency>    <groupId>com.liferay.portal</groupId>    <artifactId>portal-kernel</artifactId>    <version>5.2.3</version>    <scope>provided</scope></dependency>
2)代码如下:

import com.liferay.portal.kernel.util.ServerDetector;/** * 该类用于获取web容器的类型,如:tomcat、jetty、was、jboss等 *  * @author wm  */public class WebContainerInfo {public static String getServerName() {String serverName = null;if (ServerDetector.isWebLogic()) {serverName = "WebLogic";} else if (ServerDetector.isTomcat()) {serverName = "Tomcat";} else if (ServerDetector.isWebSphere()) {serverName = "WebSphere";} else if (ServerDetector.isSupportsComet()) {serverName = "SupportsComet";} else if (ServerDetector.isResin()) {serverName = "Resin";} else if (ServerDetector.isOC4J()) {serverName = "OC4J";} else if (ServerDetector.isJOnAS()) {serverName = "JOnAS";} else if (ServerDetector.isJetty()) {serverName = "Jetty";} else if (ServerDetector.isJBoss()) {serverName = "JBoss";} else if (ServerDetector.isGeronimo()) {serverName = "Geronimo";} else if (ServerDetector.isGlassfish()) {serverName = "Glassfish";} else if (ServerDetector.isGlassfish2()) {serverName = "Glassfish2";} else if (ServerDetector.isGlassfish3()) {serverName = "Glassfish3";}return serverName;}}







阅读全文
0 0
原创粉丝点击