获取域名/主机名有两种方法,但它们可以识别不同的内容。一个标识客户端使用的URL,另一个标识部署描述符中定义的Servlet的名称。
检索客户端使用的域:
int port = request.getServerPort();
// if port is default or 0, just use the default port.
String appUrl =
request.getScheme() + "://" + request.getServerName();
// if it's not the default port, append the port to your url
if(port != 80 || port != 443 || port != 0) {
appUrl += ":" + new Integer(port).toString();
}检索Servlet使用的主机::
String servletContextName = request.getServletContext().getServletContextName();请参阅ServletContext.getServletContextName():
public java.lang.String getServletContextName()
Returns the name of this web application correponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element.
Returns:
The name of the web application or null if no name has been declared in the deployment descriptor.
Since:
Servlet 2.3
BLOCKQUOTE>