Is there any way I can get url address of an application from java code, I mean comple开发者_Go百科te address not only value from getContextPath(). Something like http://localhost:8080/etc
Try with getRequestUrl().
I hope it helps you
In a servlet or JSP, you can call javax.servlet.http.HttpUtils.getRequestURL(request)
It returns a StringBuffer containing the entire URL up to the servlet
From the javadoc
Reconstructs the URL the client used to make the request, using information in the HttpServletRequest object. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
If you only want up to the context path, you'll have to remove your servlet path
There is no method that returns the entire URL including the query parameters. You need to use something like:
req.getRequestURL()+"?"+req.getQueryString();
Or if you don't have query parameters you can use getRequestURL
It is pretty confusing, but here is a graphic that helps sort it out:
精彩评论