Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL of the request sender with HttpServletRequest

Tags:

java

servlets

dns

How do you get the source domain using HttpServletRequest? Source domain is the requester's domain.

Thanks.

like image 586
user_1357 Avatar asked Aug 30 '11 16:08

user_1357


People also ask

How can you retrieve the full URL for the incoming request?

Retrieving the Request URI The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.

How do I request a referrer URL?

It's available in the HTTP referer header. You can get it in a servlet as follows: String referrer = request. getHeader("referer"); // Yes, with the legendary misspelling.

How do I find my Java server URL?

In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress. getHostName() to get Hostname of the current Server name.

How do I find HttpServletRequest server name?

You can use HttpServletRequest. getScheme() to retrieve either "http" or "https". Using it along with HttpServletRequest. getServerName() should be enough to rebuild the portion of the URL you need.


1 Answers

You could do either

// gets client (browser)'s hostname
String host = request.getRemoteHost(); 

OR

// get the server's domain name.
String domain = new URL(request.getRequestURL().toString()).getHost(); 
like image 79
adarshr Avatar answered Oct 03 '22 21:10

adarshr