Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

java - Getting server address and application name

Environment: NetBeans 6.9.1, GlassFish 3.1

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?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The HttpServletRequest object will give you what you need:

  • 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...