Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the domain name from the request in a Java servlet?

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.

like image 674
Bacon Avatar asked Sep 04 '11 16:09

Bacon


People also ask

How can I get request body from servlet?

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.

How would you retrieve a parameter from the servlet request?

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.

What is the GET request in servlet?

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.


1 Answers

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...

like image 61
home Avatar answered Sep 22 '22 11:09

home