Java obtains the Tomcat port number and project name without using request

CSDN Q & A 2022-02-13 06:10:05 阅读数:768

java obtains tomcat port number

In the tool class, you want to get web Project port number and project name ,
Now the port number has been obtained , How to get the project name
Do not use request
The port number is obtained in this way

MBeanServer s = MBeanServerFactory.findMBeanServer(null).get(0);Set<ObjectName> ps = s.queryNames(new ObjectName("Catalina:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));

do not know why idea Next springboot Project cannot get
eclipse Ordinary under maven The project can get

MBeanServer debuge I went in and found that there was a project name , But I don't know how to use Query Get .




Take the answer :

Get the absolute path of the project and path , The upper two levels are the project name



Other answers 2:

File name cannot be used request obtain , This is not very good


Other answers 3:

Find out MBeanServer and tomcat Of server.xml Corresponding ,server.xml Are there in connector label , also protocol The value of is HTTP/1.1.
springboot It's built in tomcat, No, server.xml To configure , So you can't get it .


Other answers 4:

application.yml It means very sad


Other answers 5:

Direct reading SpringBoot Under the yml obtain
You can read the following blog
SpringBoot Read properties/yml Content


Other answers 6:

Covered building

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130610029361.html