views:

39

answers:

3

I thought ServletContext might provide a method. Does the getAttribute() method of ServletContext provide any help i.e. is there an attribute name (maybe "host", "port") that will be of help.

The reason for this is I want my application to run wherever it is deployed, and at one point I have to allow a user to click a link that points to a location on the file server. Hence I need to reference by the host and port and cannot use an internal reference.

+3  A: 
ServletRequest.getServerName(...)
ServletRequest.getServerPort(...)
Everyone
+1  A: 

The ServletRequest object that has been passed to your doGet, or doPost method has getServerName and getServerPort methods that provide this information.

eg

public void doGet(ServletRequest request, ServletResponse response) {
    System.out.println("Host = " + request.getServerName());
    System.out.println("Port = " + request.getServerPort());
}
Tom
A: 

I have found in my old project the string:

request.getHeader("host").contains("xxx")

maybe it is the solution?