Get current url in web application

I am trying to grab the current url as it appears in the browser address bar on my JSP page and has several options to execute it.

In my current application, we are going to place a web server in front of our application server, than it seems that these values ​​will not be used.

I have another way to help javascript document.URL , but I'm not sure how reliable it will be.

I need to get information about the location of the user, and if I can use getRequestURI() , it will return something like getRequestURI() to me.

In short, all I want is to fix the URL in the address bar of the browser and save it in a hidden field of my JSP page.

I'm not sure the best way to achieve this.

+4
source share
3 answers

If you want to use the javascript solution, you can use the 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 the other parameters reading this article in MDN.

+3
source

In Java you can do this:

  public static String getCurrentUrl(HttpServletRequest request){ URL url = new URL(request.getRequestURL().toString()) String host = url.getHost(); String userInfo = url.getUserInfo(); String scheme = url.getProtocol(); String port = url.getPort(); String path = request.getAttribute("javax.servlet.forward.request_uri"); String query = request.getAttribute("javax.servlet.forward.query_string"); URI uri = new URI(scheme,userInfo,host,port,path,query,null) return uri.toString(); } 
+4
source

You can create a hidden field in your form

 <input type="hidden" id="myurl" name="myurl"/> 

then write javascript

 <script type="text/javascript"> document.getElementById('myurl').value = window.location.href </script> 

- is this help?

+2
source

Source: https://habr.com/ru/post/1415513/


All Articles