I have a problem with the query part of my URL:s on my website. I need to use Scandinavian alphabet in the query /parameters part (like [mydomain]?name=Räty). When I'm running my server code on my home computer, I have no problem with them. But when deployed to a US based hosting service, they do cause a problem.
I'm using c:url in my JSP:s and it encodes my parameters, but not Scandinavian alphabet. First question is am I supposed to encode them? Google search of URL encoding only produced instructions saying alphanumeric characters don't need to be encoded. But how about non-English alphabet?
Or is this a problem with the character encoding of my Tomcat server? I have a CharacterEncodingFilter set to UTF-8 in my Spring MVC web.xml. Do I need to do something with my Tomcat settings?
If I need to encode non-English alphabet, how do I do this in JSP and what do I need to do on server side in my Spring MVC-server code to decode the query parameters?
Can you show us some of the related code, and describe the exact problem that's being caused?
posted 11 months ago
Stephan van Hulst wrote:Can you show us some of the related code, and describe the exact problem that's being caused?
Well my question was mostly general. Am I supposed to encode those characters? I'm wondering this because <c:url> tag does do encoding but doesn't do anything to characters like Ä or Ö.
I've since tinkered with my code and I got everything to work. I did add encoding for those characters. But the code in the JSP is quite ugly. This is how I got the necessary parameters encoded:
So I used URLEncoder.encode() for encoding the parameter. But I couldn't just put it into <c:url> value attribute or the %'s would get encoded. I got around it using <c:set>. This looks way too complicated, though.
And I'm still wondering if it is generally good practice/recommended to encode those characters? Or is what I just did pointless?
In the end the problems I was having weren't due to me not encoding those characters, I think. The problem was that the production Tomcat server needed to be set to UTF-8 by adding URIEncoding="UTF-8" to the Connector tag in server.xml. This fixed it.
But I'm still wondering if I should keep the characters encoded or if its generally ok to have Ä's and Ö's and such in parameters?
posted 11 months ago
Well now I feel silly. <c:url> does take care of those Äs and Ös, the link is shown in browser as decoded text of course so I thought it did nothing to those. So it was all just a problem with server configuration. How silly. Oh well...
Helpful hint: URL syntax allows you to swap out characters for their hex values. You most commonly see this in query parameters such as "?date=next%20week", where the "%20" eliminates the problem of having a space in the middle of a URL query string.
Since the displayed values of characters in a console window can vary depending on the code page it has selected, sometimes what you type isn't what the server expects. You can eliminate this problem by using the hex value of the character that the server actually wants.
When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.
No, tomorrow we rule the world! With this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java