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.
There are two methods for reading the data in the body: getReader() returns a BufferedReader that will allow you to read the body of the request. getInputStream() returns a ServletInputStream if you need to read binary data.
getParameter() method to get the value of a form parameter. getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox. getParameterNames() − Call this method if you want a complete list of all parameters in the current request.
The request object defines a method called getAttribute() . The servlet interface provides this as a way to include extra information about the request that is not covered by any of the other HttpServletRequest methods.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With