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 ?
2 Answers
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)
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.

- 1
- 1

- 76,006
- 17
- 150
- 174
-
The problem is that port number does change. Because this is a generic service running for different spring application instances. So i do not know the port beforehand. – Shikha Jun 21 '11 at 04:07
-
I'm afraid your comment didn't make any sense. If you have multiple applications running on different containers associated with different ports, then you'll need to find a way to associate the port numbers with the reports; the point continues to remain that this info ought to be configurable. Besides, any attempt to derive the port from the request URL will fail, due to the presence of NAT solutions or reverse proxies. – Vineet Reynolds Jun 21 '11 at 04:12
-
The port is defined in a config file. Now I am fetching it from this file and setting in a variable. – Shikha Jun 21 '11 at 06:20
-
Not exactly. But a similar config file which is passed into jetty parameters. – Shikha Jun 21 '11 at 06:23
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

- 770
- 1
- 7
- 27