本文介绍了获取服务器地址和应用程序名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:NetBeans 6.9.1,GlassFish 3.1

Environment: NetBeans 6.9.1, GlassFish 3.1

我有一个Java Web应用程序。如何动态获取服务器地址和应用程序名称? '2in1'解决方案对我来说是最好的: http:// localhost:8080 / AppName /

I have a Java Web Application. How to get the server address and the application name dynamically? The '2in1' solution would be the best for me: http://localhost:8080/AppName/.

有没有实用的方法来获取这些信息?

Is there a practical way to get that information?

假设 AppName 的值将被修复,所以我只需要主机地址。是否可以通过JMX检索它?还有其他方法吗?

Let's say the value of AppName will be fixed, so I only need the host address. Is it possible to retrieve it via JMX? Any other ways?

推荐答案

HttpServletRequest 对象会给你什么需要:

The HttpServletRequest object will give you what you need:


  • HttpServletRequest#getLocalAddr():服务器的IP地址为字符串

  • HttpServletRequest#getLocalName():接收请求的服务器的名称

  • HttpServletRequest#getServerName():请求发送到的服务器的名称

  • HtppServletRequest#getLocalPort( ):服务器收到请求的端口

  • HttpServletRequest#getServerPort():端口请求已发送至

  • HttpServletRequest#getContextPath():标识应用程序的路径部分

  • HttpServletRequest#getLocalAddr(): The server's IP address as a string
  • HttpServletRequest#getLocalName(): The name of the server receiving the request
  • HttpServletRequest#getServerName(): The name of the server that the request was sent to
  • HtppServletRequest#getLocalPort(): The port the server received the request on
  • HttpServletRequest#getServerPort(): The port the request was sent to
  • HttpServletRequest#getContextPath(): The part of the path that identifies the application

这篇关于获取服务器地址和应用程序名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 10:11
查看更多