欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java获取web容器地址的方法

程序员文章站 2023-12-01 14:06:04
tomcat本地地址 e:\soft4develop\apache-tomcat-6.0.18 system.getproperty("user.dir")//e:\so...

tomcat本地地址 e:\soft4develop\apache-tomcat-6.0.18

system.getproperty("user.dir")//e:\soft4develop\apache-tomcat-6.0.18\bin
system.getproperty("catalina.home")//e:\soft4develop\apache-tomcat-6.0.18对于jboss同样适用。其他容器未做测试。

说道这里,正好有朋友在群里头问了个问题,情景式这样的

登陆需要用https来做请求,登陆成功后,剩下的其他请求全部走http.

比如https://www.jb51.net/admin/user_manager.apsx

发现这个不需要走https,就转发到

//www.jb51.net/admin/user_manager.apsx

问题来了

request.getserverport() 只能获取https时的port1端口

那如何获取port2端口呢。

通过上面的方式可以获取到tomact的路径,在通过下面xml的xpath来获取到

tomcat的server.xml中的端口配置拼接,来实现。

获取tomcat端口的方法

复制代码 代码如下:

 public static integer gettomcatportfromconfigxml(file serverxml) {
    integer port;
    try {
       documentbuilderfactory domfactory = documentbuilderfactory.newinstance();
       domfactory.setnamespaceaware(true); // never forget this!
       documentbuilder builder = domfactory.newdocumentbuilder();
       document doc = builder.parse(serverxml);
       xpathfactory factory = xpathfactory.newinstance();
       xpath xpath = factory.newxpath();
       xpathexpression expr = xpath.compile
         ("/server/service[@name='catalina']/connector[count(@scheme)=0]/@port[1]");
       string result = (string) expr.evaluate(doc, xpathconstants.string);
       port =  result != null && result.length() > 0 ? integer.valueof(result) : null;
    } catch (exception e) {
      port = null;
    }
    return port;
 }

最后附带下system.ge
复制代码 代码如下:

另外:system.getproperty()中的字符串参数如下:

system.getproperty()参数大全
# java.version                                java runtime environment version
# java.vendor                                java runtime environment vendor
# java.vendor.url                           java vendor url
# java.home                                java installation directory
# java.vm.specification.version   java virtual machine specification version
# java.vm.specification.vendor    java virtual machine specification vendor
# java.vm.specification.name      java virtual machine specification name
# java.vm.version                        java virtual machine implementation version
# java.vm.vendor                        java virtual machine implementation vendor
# java.vm.name                        java virtual machine implementation name
# java.specification.version        java runtime environment specification version
# java.specification.vendor         java runtime environment specification vendor
# java.specification.name           java runtime environment specification name
# java.class.version                    java class format version number
# java.class.path                      java class path
# java.library.path                 list of paths to search when loading libraries
# java.io.tmpdir                       default temp file path
# java.compiler                       name of jit compiler to use
# java.ext.dirs                       path of extension directory or directories
# os.name                              operating system name
# os.arch                                  operating system architecture
# os.version                       operating system version
# file.separator                         file separator ("/" on unix)
# path.separator                  path separator (":" on unix)
# line.separator                       line separator ("\n" on unix)
# user.name                        user's account name
# user.home                              user's home directory
# user.dir                               user's current working directory


file.getcanonicalpath()和file.getabsolutepath()大约只是对于new file(".")和new file("..")两种路径有所区别。

复制代码 代码如下:

# 对于getcanonicalpath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
# 对于getabsolutepath()函数,则不管”.”、“..”,返回当前的路径加上你在new file()时设定的路径
# 至于getpath()函数,得到的只是你在new file()时设定的路径

比如当前的路径为 c:\test :
file directory = new file("abc");
directory.getcanonicalpath(); //得到的是c:\test\abc
directory.getabsolutepath();    //得到的是c:\test\abc
direcotry.getpath();                    //得到的是abc

file directory = new file(".");
directory.getcanonicalpath(); //得到的是c:\test
directory.getabsolutepath();    //得到的是c:\test\.
direcotry.getpath();                    //得到的是.

file directory = new file("..");
directory.getcanonicalpath(); //得到的是c:\
directory.getabsolutepath();    //得到的是c:\test\..
direcotry.getpath();                    //得到的是..