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
Related
I am working on a Spring Boot application
I need to make a request to an external service, old and ill-conceived. The request take the form of a HTTP GET (or POST) call, but the payload, an xml content, need to be passed as a query parameter. For example,
GET http://ill-service.com/plain.cgi?XML_DATA=<request attribute="attributeValue"><content contentAttribute="plain"/></request>
Of course, the value of query param XML_DATA need to be URL encoded, and normally, the RestTemplate of Spring boot work good on that, following RFC 3986 (see http://www.ietf.org/rfc/rfc3986.txt).
Except that, as allowed by this RFC, '/' and '=' character are left in the param value, giving me the following query :
GET http://ill-service.com/plain.cgi?XML_DATA=%3Crequest%20attribute=%22attributeValue%22%3E%3Ccontent%20contentAttribute=%22plain%22/%3E%3C/request%3E
In a perfect wold, this would be good, but do you remember when I said that the service I am trying to call is ill-conceived ? In another world, it needs to have the full content of XML_DATA URL-encoded. In another words, it needs the following query:
GET http://ill-service.com/plain.cgi?XML_DATA=%3Crequest%20attribute%3D%22attributeValue%22%3E%3Ccontent%20contentAttribute%3D%22plain%22%2F%3E%3C%2Frequest%3E%0A
I am quite lost on how to instruct the rest template or the UriComponentBuilder I am using to do so. Any help would be greatly appreciated
Probably u can use spring's UriUtils class
Use java.net.URLEncoder to encode your XML payload first and then append the encoded payload.
Following the suggestion of Vasif, and some information about UriComponentBuilder I found the following solutions :
String xmlContent = "<request attribute="attributeValue"><content contentAttribute="plain"/></request>";
URI uri = UriComponentsBuilder.fromHttpUrl("http://ill-service.com/plain.cgi")
//This part set the query param as a full encoded value, not as query value encoded
.queryParam("XML_DATA", UriUtils.encode(xmlContent, "UTF-8"))
//The build(true) indicate to the builder that the Uri is already encoded
.build(true).toUri();
String responseStr = restTemplate.getForObject(uri ,String.class)
I did a web application in Java on Tomcat container. I have a url string
http://XXXXXXXXXX:8082/App/login#access_token=b8ee9e9707c54a8e23bdc224dfdae&expires_in=3600
How can I parse that string and read a hash value (the value after #) from HttpServletRequest?
Note : the URL string is from HttpServletRequest, I didn't get this full URL dirctly from request by request.getRequestURI
Thanks for any suggestion
String temp ="http://XXXXXXXXXX:8082/App/login#access_token=b8ee9e9707c54a8e23bdc224dfdae&expires_in=3600";
temp.substring(temp.lastIndexOf("#"), temp.indexOf("&", temp.lastIndexOf("#")));
cheers!
I think it is having all you need:
here
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;
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.
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: