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

问题背景:最近在开发一个人行的项目,由于不同的商行所使用的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;
	}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值