使用java 获取tomcat服务器端口及应用服务器IP地址

本文展示了如何使用Java通过MBeanServer获取Tomcat的HTTP/1.1 Connector端口,并介绍了如何遍历网络接口获取服务器的实际IP地址,提供了一种实用的IT运维解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、获取Tomcat端口

  MBeanServer bs = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> obname= null;
        try {
            obname = bs
                .queryNames(new ObjectName("*:type=Connector,*"), Query
                    .match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        port = obname.iterator().next().getKeyProperty("port");
        System.out.println(port); 

        2、获取应用服务器IP地址

        for (Enumeration<NetworkInterface>  enumeration = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface networkinterface = enumeration.nextElement();
            if (!name .contains("docker") && !name.contains("lo")) {
                for (Enumeration<InetAddress> iparr = networkinterface.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress address = iparr.nextElement();
                    if (!address.isLoopbackAddress()) {
                        String ipaddress = address.getHostAddress().toString();
                        if (!ipaddress.contains("::") 
                                && !ipaddress.contains("0:0:") 
                                && !ipaddress.contains("fe80")) {
                            System.out.println(ipaddress);
                        }
                    }
                }
            }
        } 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值