Java method to get the web container address

  • 2020-04-01 01:37:52
  • OfStack

Tomcat local address 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 also works for jboss. The other containers were not tested.

At this point, a friend asked a question in the group

The login needs Https to make the request, and once the login is successful, the rest of the requests go over HTTP.

Such as https://www.jb51.net/admin/user_manager.apsx

If you find that you don't need to go Https, forward to

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

The question

Request-getserverport () can only get the port1 port when HTTPS is used

So how do you get the port2 port.

The path to tomact can be obtained in the above way and obtained through the xpath of the following XML

Tomcat's server.xml port configuration splicing to achieve.

Method to get the tomcat port


 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;
 }

Finally, System. Ge

 In addition: System.getProperty() The string parameters are as follows: 
System.getProperty() Parameters of 
# 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 () and file.getabsolutepath () are roughly just for new File(".") and new File("..") ) the two paths are different.


#  for getCanonicalPath() Function." ." Represents the current folder, and" .. "Represents the folder above the current folder 
#  for getAbsolutePath() Function, regardless of" . "," .. ", returns the current path plus you in new File() Is the path set by 
#  As for the getPath() The delta function, all you get is the delta function new File() Is the path set by 
 For example, the current path is  C:test  : 
File directory = new File("abc");
directory.getCanonicalPath(); //You get C:test ABC
directory.getAbsolutePath();    //You get C:test ABC
direcotry.getPath();                    //You get ABC
File directory = new File(".");
directory.getCanonicalPath(); //You get C:test
directory.getAbsolutePath();    //You get C:test.
direcotry.getPath();                    //What you get is.
File directory = new File("..");
directory.getCanonicalPath(); //You get C:
directory.getAbsolutePath();    //You get C:test..
direcotry.getPath();                    //What you get is..


Related articles: