Java获得Web容器类型

今天需要获得Java运行时候web容器的类型,例如Tomcat或JBoss,利用了portal-kernel 的ServerDetector,实例如下:需要添加portal-kernel.jar 包
import com.liferay.portal.kernel.util.ServerDetector;

public class TestSystemInfo {

	public static void main(String[] args) {	
		ServerName();
	}
	public static String ServerName(){
		
		String serverName = null;
		if (ServerDetector.isWebLogic()) { //
			serverName = "WebLogic";
			System.out.println("WebLogic");
		} else if (ServerDetector.isTomcat()) {//
			serverName = "Tomcat";
			System.out.println("Tomcat");
		} else if (ServerDetector.isWebSphere()) { //
			serverName = "WebSphere";
			System.out.println("WebSphere");
		} else if (ServerDetector.isSupportsComet()) { //
			serverName = "SupportsComet";
			System.out.println("SupportsComet");
		} else if (ServerDetector.isResin()) { //
			serverName = "Resin";
			System.out.println("Resin");
		} else if (ServerDetector.isOC4J()) { //
			serverName = "OC4J";
			System.out.println("OC4J");
		} else if (ServerDetector.isJOnAS()) { //
			serverName = "JOnAS";
			System.out.println("JOnAS");
		} else if (ServerDetector.isJetty()) { //
			serverName = "Jetty";
			System.out.println("Jetty");
		} else if (ServerDetector.isJBoss()) { //
			serverName = "JBoss";
			System.out.println("JBoss");
		} else if (ServerDetector.isGeronimo()) { //
			serverName = "Geronimo";
			System.out.println("Geronimo");
		} else if (ServerDetector.isGlassfish()) { //
			serverName = "Glassfish";
			System.out.println("Glassfish");
		} else if (ServerDetector.isGlassfish2()) { //
			serverName = "Glassfish2";
			System.out.println("Glassfish2");
		} else if (ServerDetector.isGlassfish3()) { //
			serverName = "Glassfish3";
			System.out.println("Glassfish3");
		}		
		System.out.println(serverName);
		return serverName;
	}

}


 项目运行的时候调用ServerName()方法就可以了 
  
 
 

你可能感兴趣的:(JAVA,java,web,tomcat,jboss,kernel)