Get current URL in Webapplication

jakarta-ee, java, javascript, jsp

Solution

If you want a javascript solution, you can use window.document.location object and its properties:

console.log(window.document.location.protocol);
http:
console.log(window.document.location.host);
stackoverflow.com
console.log(window.document.location.port);

console.log(window.document.location.href);
http://stackoverflow.com/questions/10845606/get-current-url-in-webapplication
console.log(window.document.location.pathname);
/questions/10845606/get-current-url-in-webapplication

You can understand other parameters reading this article at MDN.

Problem

I am in process to capture Current URL as its being displayed in the browser's address bar in my JSP page and have few options to get it done. - Using `javax.servlet.include.request_uri` and other defined in Servlet 2.4 specification.I refer this thread to get details about it java-httpservletrequest-get-url-in-browsers-url-bar. In my current application, we are going to put web-server in front of our application server as than it seems that those values will be of not any use. I have another way to take help of javascript's `document.URL` but i am not sure how reliable it is going to be. I need to get the details about the location of the user and if I can use `getRequestURI()`, it will return me something like `www.abc.com/abc/search.jsp`. In short, all I want to capture the URL being there in the address bar of the browser and save it in a hidden field of my JSP page. I am not sure what is the best way to achieve this.

Original source

Related problems