我有一个可以在 tomcat 和 jboss 上运行的 java 应用程序。
我需要执行“if 条件”来根据网络服务器类型执行某些任务。
我如何访问这些信息?
我需要这样做,因为我需要连接到数据源,并且我可以根据网络服务器以不同的方式获取上下文:
try{
String webserver = getWebServer();
Logger logger=Logger.getLogger("myLog");
if(webserver.equalsIgnoreCase("Jboss")){
logger.severe("Webserver: " + webserver);
Hashtable<String, String> ht= new Hashtable();
ht.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
ht.put(InitialContext.PROVIDER_URL,"jnp://localhost:1099");
ht.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
InitialContext ic=new InitialContext(ht);
if(ic!=null){
logger.severe("success");/*I am getting success as output here*/
DataSource ds=(DataSource)ic.lookup(getDatasource());
/*this is where it's failing*/
if(ds!=null){
logger.severe("success1");
}
return ds.getConnection();
}
return null;
}
else{ //TOMCAT
logger.severe("Webserver: " + webserver);
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource) envCtx.lookup(getDatasource());
// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
return conn;
}
}
Tomcat 版本不适用于 jboss,反之亦然 谢谢!
最佳答案
由于您的应用程序可以在 Tomcat(一个简单的 servlet 容器)上运行,因此您可能需要使用 ServletContext#getServerInfo() 。获取有关应用程序运行环境的信息。
关于java - 应用程序运行在哪个网络服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21475613/