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 is also applicable to jboss. Other containers are not tested.
A friend asked a question in the group.
Https is required for login. After successful login, all other requests go through http.
Like https://www.jb51.net/admin/user_manager.apsx
If you find that you do not need to use Https, you will be forwarded
Http://www.jb51.net/admin/user_manager.apsx
The problem arises.
Request. getServerPort () can only obtain the port port1 at https.
Then how can we obtain Port 2.
The path of tomact can be obtained through the above method, and obtained through the xml xpath below
The port configuration in tomcat server. xml is spliced.
How to obtain tomcat Port
Copy codeThe Code is as follows: 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;
}
The System. geCopy codeThe Code is as follows: In addition, the string parameter in System. getProperty () is as follows:
System. getProperty () Parameters
# 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 only different for new File (".") and new File.
Copy codeThe Code is as follows: # For the getCanonicalPath () function, "." indicates the current folder, and ".." indicates the upper-level folder of the current folder.
# For the getAbsolutePath () function, no matter "." Or ".", the current path is returned and the path you set when new File () is added.
# As for the getPath () function, all you get is the path you set in new File ().
For example, the current path is C: \ test:
File directory = new File ("abc ");
Directory. getCanonicalPath (); // C: \ test \ abc
Directory. getAbsolutePath (); // C: \ test \ abc
Direcotry. getPath (); // abc is returned.
File directory = new File (".");
Directory. getCanonicalPath (); // C: \ test
Directory. getAbsolutePath (); // C: \ test \.
Direcotry. getPath (); // The result is.
File directory = new File ("..");
Directory. getCanonicalPath (); // The returned result is C :\
Directory. getAbsolutePath (); // C: \ test \..
Direcotry. getPath (); // The result is ..