server.xmlのコンフィグレーションでrequest.getServerName()またはrequest.getLocalAddr()でtomcatの応答を操作することができますか?
サーバーが異なるサブネットワーク内の異なるIPで知られているため、これが必要です。
server.xmlのコンフィグレーションでrequest.getServerName()またはrequest.getLocalAddr()でtomcatの応答を操作することができますか?
サーバーが異なるサブネットワーク内の異なるIPで知られているため、これが必要です。
私が知る限り、web.xmlの設定を変更するだけでServletRequest.getServerName()またはServletRequest.getLocalAddr()が返す値を変更することはできません。
しかし、ServletRequest/HttpServletRequestラッパーを作成すると、標準以外の値を返す場合を除き、すべてのメソッド呼び出しを元の要求に委譲できます。あなたのアプリケーションに来るすべてのリクエストをラップするには、Filterを実装することができます。このリクエストは受信したリクエストをラップし、ラッパーをフィルタチェーンに渡します。フィルタはweb.xmlに設定されます。このフィルタの後のすべては、透過的にラッパーを使用し、指定したカスタム値を取得します。
ラッパーは、次のようになります。あなたはこのTomcatインスタンスで配信されていた各サブドメインの仮想ホストを設定しようとすることができ
public class RequestWrapperFilter implements Filter {
private final String serverName;
public void init(FilterConfig filterConfig) {
this.serverName = filterConfig.getInitParameter("serverName");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
WrappedRequest wrapped = new WrappedRequest(request);
wrapper.setServerName(this.serverName);
chain.doFilter(wrapped, response);
}
public void destroy() { }
}
:
public class WrappedRequest implements ServletRequest {
private final ServletRequest original;
private String customServerName;
public WrappedRequest(ServletRequest original) {
this.original = original;
}
// ... delegate all method calls to original
public String getServerName() {
if (this.customServerName != null) {
return this.customServerName;
}
return this.original.getServerName();
}
public void setServerName(String customServerName) {
this.customServerName = customServerName;
}
}
フィルタ。
異なるIPまたは異なるホスト名によってのみですか? – Gandalf