URL that contain the ? is changed automaticaly to %3F with thymeleaf - java

I read many URLs from database through Java Spring Boot and thymleaf expression. And if the URL has "?" it changed automaticaly to "%3F" for instance the URL: www.myserver.de?id=3 will be www.myserver.de%3Fid=3 I think I can't use this solution link because the URLs are readen from DB with Params
< a th:href="#{//{link}(link=${link})}" target="_blank"> For more information click here < /a >

Your URLs are url encoded in the database so ? is represented as %3F. If you want to get the original URL from encoded URL you need to url decode it e.g. by doing:
String result = java.net.URLDecoder.decode(url, StandardCharsets.UTF_8);

You can decode it back to what was your original url.
If you are using java use java.net.URLDecoder.
More here

If you are just reading complete URLs straight from the database, you shouldn't be using Thymeleaf standard url syntax (as it's designed to escape URL parameters). You should just do something like:
<a th:href="|//${link}|" target="_blank">For more information click here</a>

Related

Thymeleaf: Unencode or Encode URL on client

I am using Thymeleaf as the front end.
I have some menu categories in natural language that I display on a web page and pass to the server.
For example, I have a category of "My favourite cats"
The category is in a variable ${category.key}
This category has a link;
<a th:href="|http://myserver?selectedCategory=${category.key}|><span th:text=${category.key}></span></a>
If I do not URLEncode ${category.key} on the server, then when a
user clicks the link, the selectedCategory parameter is null if there
is a whitespace in the category string.
If I encode the category string, the selectedCategory parameter
passes to the server fine BUT the link text appears as
My+favourite+cats
I don't want two variables, one encoded, one not encoded.
How do I either encode or unencode ${category.key} as part of the Thymeleaf HTML compilation process?
#symbol is Server Context path in Thymeleaf. you use #symbol.
<a href="#" th:href="#{/your server context path?selectedCategory=__${category.key}__}">
<span th:text="${category.key}"> </span></a>
Thymeleaf will url encode strings if you build them with url syntax (which was supplied in a comment above). For your example url, it should look like this -- using the # symbol instead of the $:
<a th:href="#{http://myserver(selectedCategory=${category.key})}" th:text="${category.key}" />
(Also, you don't need that extra span.)
Using the #symbol is definitely the primary alternative. But just to note another option that may be of use on occasions:
The answer is to use the lesser know #strings functions to replace the '+' with ' '
In the link text, use
th:text="${#strings.replace(category.key,'+',' ')}"

How to convert url encoded string to plain text in JAVA

I am looking for html references in a HTML page I am retrieving from the server. The problem is for all the hyperlinks I am retrieving, the text that I am getting is URL encoded. Lets say, the URL is "http://abc.def.com/gh?ij=x&kl=y&mn=z", my program parses it as "http://abc.def.com/gh?ij=3Dx&kl=3Dy&mn=3Dz" . (look at the difference around "=" and "&" in the two URL's) . Some searching on the Web tells me that the second URL is a URL encoded form of the first URL.
What should I do to retrieve the actual URL as it is, and not its URL encoded version? Right now, I am replacing =3D with 3D and & with &, but that is a very bad hack.
Try to use java.net.URLDecoder

Extracting relative links from a web page in proper format using Jsoup

I have parsed the outlinks of a web page which I am going to parse again using Jsoup. But the problem is that, the links are of the form: ../../../pincode/india/andaman-and-nicobar- islands/. In this form I cannot parse them. So I have converted to absolute url using link.attr("abs:href") with the help of other post of stackoverflow.
Url of the first web page that I have parsed is: http://www.mapsofindia.com/pincode/india/. And the absolute URls that I have got after parsing is of the form http://www.mapsofindia.com/../pincode/india/andaman-and-nicobar-islands/. But I cannot parse them further using Jsoup. So when I am executing the following statement:
Jsoup.parse("http://www.mapsofindia.com/../pincode/india/andaman-and-nicobar-islands/");
It is giving HTTP 400 error i.e. bad request. So I think there is some problem with the Urls. So can anyone please help me to solve the above problem to get the urls in proper manner so that I can parse them further. Thank you.
please test these two things:
try using link.absUrl("href") instead of link.attr("abs:href")
Check the base uri (calling baseUri() on your element or document)
Btw. you better use connect() Method for this thing:
Document doc = Jsoup.connect("http://<your url here>").get();

I am being asked to encode a URL. How do I do this?

I am being asked to encode a header image parameter into a base url but I am not sure how to do this. Here is a screenshot of the document they have supplied me: http://postimage.org/image/6hkscqdld/
Basically they have a page that I am suppose to use, but I can add a custom header image to that page by adding a parameter to the URL, however the dynamic header image tag must be encoded. How would I go about encoding it?
in php you can just use the function
urlencode($string);
http://php.net/manual/en/function.urlencode.php
$url = 'http://www.example.com/your/url/goes/here';
urlencode($url);
The above will take a string and encode it properly for use in a URL.

Java servlet sendRequest - getParameter encoding Problem

I'm building a web app for my lesson using java servlets. At some point i want to redirect to a jsp page, sending also some info that want to use there (using the GET method).
In my servlet i have the following code:
String link = new String("index.jsp?name="+metadata.getName()+"&title="+metadata.getTitle());
response.sendRedirect(response.encodeRedirectURL(link));
In the jsp, I get these parameters using
<%
request.getParameter("name");
request.getParameter("title");
%>
Everything works fine, except when the parameters do not contain only latin characters (in my case they can contain greek characters).
For example if name=ΕΡΕΥΝΑΣ i get name=¡¥.
How can i fix this encoding problem (setting it to UTF-8)?
Isn't encodeRedirectURL() doing this job? Should I also use encodeURL() at some point? I tried the last one but problem still existed.
Thanks in advance :)
The HttpServletResponse#encodeRedirectURL() does not URL-encode the URL. It only appends the jsessionid attribute to the URL whenever there's a session and the client has cookies disabled. Admittedly, it's a confusing method name.
You need to encode the request parameters with help of URLEncoder#encode() yourself during composing the URL.
String charset = "UTF-8";
String link = String.format("index.jsp?name=%s&title=%s",
URLEncoder.encode(metadata.getName(), charset),
URLEncoder.encode(metadata.getTitle(), charset));
response.sendRedirect(response.encodeRedirectURL(link));
And create a filter which is mapped on /* and does basically the following in doFilter() method:
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
And add the following to top of your JSP:
<%# page pageEncoding="UTF-8" %>
Finally you'll be able to display them as follows:
<p>Name: ${param.name}</p>
<p>Title: ${param.title}</p>
See also:
Unicode - How to get characters right?
Use the java.net.URLEncoder to encode each parameter before adding them to the url. Think about it this way: if your name contained a "&", how would you know that this was not a parameter delimiter?
You should encode every request parameter with URLEncoder.encode() before putting it into the query string .
The encodeRedirectURL method is only used to include the session ID into the URL if necessary (URL rewriting if no cookie support by the browser)
How about the following instead?
Set the name and title as attributes on the request object;
Get a request dispatcher for the JSP from the request object or via the servlet context;
Use the request dispatcher to forward the request to the JSP;
Access these attributes in the request from the JSP.
This saves redirecting the browser from the first servlet to the JSP derived servlet and avoids the whole parameter encoding issue entirely.
Also ensure the JSP page directive sets the content encoding to UTF-8.

Categories

Resources