How would I get the full URL of a JSP page.
For example the URL might be http://www.example.com/news.do/?language=nl&country=NL
If I do things like the following I always get news.jsp and not .do
out.print(request.getServletPath());
out.print(request.getRequestURI());
out.print(request.getRequest());
out.print(request.getContextPath());
You need to call request.getRequestURL():
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
Given URL = http:/localhost:8080/sample/url.jsp?id1=something&id2=something&id3=something
request.getQueryString();
it returns id1=something&id2=something&id3=something
See This
I found a solution. It may not be perfect solution. But it working solution.
String qs = request.getQueryString();
String foo = request.getRequestURL().toString()+"?"+qs;
Related
Is it possible to get everything after the root domain name from an HTTP request using Java?
So if the url being requested is http://example.com/my-path then I'd like to get the value of my-path
I'm using the PlayFramework so if there's a header called "Path" or something like that it should be easier to get it with:
String path = request.headers.get("path");
But this page suggests that such a thing doesn't exist:
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
If there's a different solution to get the same result, that would also be appreciated. If it is possible it will provide a solution to this question as well:
http://stackoverflow.com/questions/28503129/redirect-example-com-to-www-example-com-in-playframework-1-2-x
To get everything after the domain, use uri:
String path = request.uri();
Or if you want to cut out query string parameters, use path:
String path = request.path();
I am trying to extract the query string from the static URL from the browser using Java.
But when I use the getQueryString() method, it is returning the query string for the particular portlet.
For example: my static url looks like this /owner/?search&name= and the portlet URL is /home/create which can't be seen in the browser URL.
Is there any way to extract the query string from Static URL?
James,
You may try to use PortletRequest.getParameterMap(), returning a java.util.Map of java.lang.String,java.lang.String[] - as in the official JSR-286 spec
Check the link, there are some other methods to extract URL parameters and values.
Hope this helps,
Philippe
I should use link. And it means that method GET and all parameters I can see in url. How I can hide parametrs from url like that
http://localhost:8080/MyApp/action.do?method=delete&id=0
And how after that I can get parametrs in java class.
You can't hide GET parameters from a URL. GET name value pairs have to be part of a request. However one thing you can certainly try is to encrypt your querystring into a string which only your code can decode on your host, for example
http://localhost:8080/MyApp/action.do?method=delete&id=0
could be
http://localhost:8080/MyApp/action.do?param=[ENCRYPTED/OBFUSCATED STRING]
then your application at the above url can decode that and convert back into name value pairs
That's the idea of GET method - to path parameters through URL, probably you should use POST.
From the following URL in OathCallBack page I want extract access_token and token_type using Java. Any idea how to do it?
http://myserver.com/OathCallBack#state=/profile&access_token=ya29.AHES6ZQLqtYrPKuw2pMzURJtWuvINspm8-Vf5x-MZ5YzqVy5&token_type=Bearer&expires_in=3600
I tried the following, but unable to extract required information.
{
String scheme = req.getScheme(); // http
String serverName = req.getServerName(); // myserver.com
int serverPort = req.getServerPort(); // 80
String contextPath = req.getContextPath();
String servletPath = req.getServletPath();
String pathInfo = req.getPathInfo(); // return null and exception
String queryString = req.getQueryString(); // return null
}
<---------------------------------------------------------->
I am going to edit my question
Thank you every one for nice reply,
google did it,
you can refer to that link by URL
http://developers.google.com/accounts/docs/OAuth2Login
inside above URL page there is following link
http://accounts.google.com/o/oauth2/auth? scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile& state=%2Fprofile& redirect_uri=https%3A%2F%2Foauth2-login-demo.appspot.com%2Foauthcallback& response_type=token& client_id=812741506391.apps.googleusercontent.com
when you click on above link, then you will get your gmail login account access_token, and that token is after # sign
Some characters cannot be part of a URL (for example, the space) and some other characters have a special meaning in a URL: for example, the character # can be used to further specify a subsection (or fragment) of a document; the character = is used to separate a name from a value.
see http://en.wikipedia.org/wiki/Query_string for more:
It looks like the '#' should be a '?'.
In a normal URL, the parameters are passed as key value pairs following a '?' and multiple parameters chained together using '&'. A URL might look as follows:
http: //someserver.com/somedir/somepage.html?param1=value1¶m2=value2¶m3=value3.
Normally the Java servlet container would return everything after the '?' when calling getQueryString() but due to the absence of the '?' it returns null.
As #Sandeep Nair has suggested getRequestURL() should return this full URL to you and you could parse it using regular expressions to get the information you want. A possible regular expression to use would be along the lines of:
(?<=access_token=)[a-zA-Z0-9.-]*
However, getRequestURL() does NOT normally return the query string, so using this method is relying on the fact that there is a '#' rather and a '?' and is therefore probably not a great solution. See here.
I would advise that you find out why you are getting a '#' instead of a '?' and try to get this changed, if you can do this then the servlet container should manage the URL parameters for you and call to request.getAttribute("access_token") and request.getAttribute("token_type") (see here) will return both values as strings.
You get query string by calling
String queryString = req.getQueryString();
It correctly returns null in your case, as there is no query string. The characters after "#" are anchor specification, which is only visible to the browser and not sent to server.
Is there any way I can get url address of an application from java code, I mean complete 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: