Fetch application port without access to request object
I have a back end process which is initiated on server startup. After completion of this process I send an email to 开发者_运维知识库users. I need access to application port in order to construct the complete URL of a web report running on same instance. Is there a way to fetch the port without access to request object ?
I need access to application port in order to construct the complete URL of a web report running on same instance. Is there a way to fetch the port without access to request object ?
Why don't you store this as a property in a configuration file to your back-end process? The code will turn out to be much simpler if just need just the port (or even a website URL fragment that doesn't change) that needs to be embedded in an email.
Besides, the administrators in question have better things to do than change port numbers on a periodic basis.
Related questions (but not quite the same)
- Root URl of the servlet
- Finding your application's URL with only a ServletContext
Both of them provide reasons, why the path seen by a servlet container, need not be the same as the one seen by an end user. Even if both are the same, there is no way, one can access this information without
- reading the server configuration files. In this case, it would be
$JETTY_HOME/etc/jetty.xml
, which has the necessary info in theConnectors
elements. - obtaining a reference to a
ServletRequest
object, which might not be a valid answer to this question.
If you have at least one http request call in your application, you could get port from it:
int portNumber = request.getServerPort();
Maybe your startup script can make such call.
More details: http://www.kodejava.org/examples/211.html
精彩评论