最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Get current URL in Webapplication - Stack Overflow

programmeradmin1浏览0评论

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

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.

Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked Jun 1, 2012 at 6:45 Umesh AwasthiUmesh Awasthi 23.6k38 gold badges138 silver badges209 bronze badges 3
  • 1 You want "about the location of the user" and getRequestURI(). Question is not very clear. Can u make it simple what do you want. – Ramesh PVK Commented Jun 1, 2012 at 6:49
  • 1 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 – Umesh Awasthi Commented Jun 1, 2012 at 6:54
  • Write the getRequestURI() in the jsp. That should do right. – Ramesh PVK Commented Jun 1, 2012 at 7:02
Add a comment  | 

3 Answers 3

Reset to default 9

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();
}

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.

You can create a hidden field in your form

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

then write a javascript

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

is that help?

发布评论

评论列表(0)

  1. 暂无评论