サーバ名、接続ホスト名の取得


以下、動作しているサーバと接続元ホストを出力するサンプル。


hostLab.jsp

<%@ page contentType= "text/html; charset=MS932" %>

<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpSession" %>
<%@ page import="java.net.InetAddress" %>

<html>

<form name = "form1">


	ServerName -> 
	<%= InetAddress.getLocalHost().getHostName() %>
	<br>

	ServerAddress -> 
	<%= InetAddress.getLocalHost().getHostAddress() %>
	<br>



	RemoteHostAddress -> 
	<%= request.getRemoteAddr()%>
	<br>

	RemoteHostName 1-> 
	<%= request.getRemoteHost()%>
	<br>

	RemoteHostName 2 -> 
	<%= InetAddress.getByName(request.getRemoteAddr()).getHostName() %>
	<br>


</form>
</html>

request.getRemoteHostでホスト名を取得するには
server.xmlにあるConnectorタグのenableLookups属性をtrueにしておく必要がある。(Tomcatの場合)

<Connector className="org.apache.coyote.tomcat4.CoyoteConnector" acceptCount="64" connectionTimeout="480000" enableLookups="true" maxKeepAliveRequests="0" maxProcessors="65" minProcessors="16" useURIValidationHack="false">