如何从Java servlet中的请求中获取域名? [英] How to get the domain name from the request in a Java servlet?

查看:1124
本文介绍了如何从Java servlet中的请求中获取域名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个servlet提供来自两个域 example1.com example2.com 的数据,你如何检索来自Java servlet中请求的域信息?



目的是根据域执行不同的操作。

解决方案

很简单, javax.servlet.ServletRequest.getServerName()。它还提供其他方法以检索请求信息, getScheme() getServerPort() ...



<编辑:如果您有一个Web服务器保护您的应用程序服务器,它必须配置为支持它,否则 getServerName()返回您的应用程序服务器的名称是你想要的......


If a single servlet serves data from two domains example1.com and example2.com, how do you retrieve the domain information from the request in a Java servlet?

The purpose is to perform different actions depending on the domain.

解决方案

Very easy, javax.servlet.ServletRequest.getServerName(). It also provides further methods to retrieve request information, getScheme(), getServerPort()...

Edit: If you have a web server guarding your application server, it must be configured to support this, otherwise getServerName() returns the name of your application server which may not be what you want...

这篇关于如何从Java servlet中的请求中获取域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆