问题
I am trying to pass the Euro ( € ) sign as url parameter in my spring jsp. What is the right way to do so ? I tried the following with no avail. Problem is the character is getting encoded properly but not getting decoded from my destination jsp.
I am using
<%@page contentType="text/html;charset=UTF-8" %>
Here is the calling jsp:
<script>
...
// params contains the euro sign
document.location='dest.jsp?p='+escape(params);
In the dest.jsp
<input type="hidden" id="par" value="${param.p}">
and in a script in the same page
console.log($('#par').val())
- when I use escape(params) I get the url as %u20AC . But no (empty) values in the dest.jsp
- when I use encodeURI(params) or encodeURIComponent I get url as € . But the value in dest.jsp as
â¬
- something which I can't use to render as euro sign
回答1:
I'm going to assume you are using Tomcat because that's what I tested with and we get the same result.
What you will want to do is open up your Tomcat servlet.xml
file and find the HTTP connector and add the useBodyEncodingForURI attribute with the value true
.
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
redirectPort="8443" useBodyEncodingForURI="true">
</Connector>
Then, you will want to register a CharacterEncodingFilter
to set the HttpServletRequest
character encoding.
You can read more about this behavior in my answer here:
- Character encoding in query string, hebrew
回答2:
You need indeed to encode the € sign which should give %E2%82%AC
using UTF-8
. You need to be careful with the encoding you use on both ends.
Something like URLEncoder.encode(url, "UTF-8")
on the client would do.
If you are using Spring, org.springframework.web.util.UriUtils
has also nice utilities you can use.
If the decoding issue is on the server, you need first to make sure that your web container decodes the URI with the proper encoding.
Tomcat decodes URI with ISO-8859-1
by default so you need to update your connector configuration
<Connector port="8080" ...
URIEncoding="UTF-8"/>
回答3:
See the following answers
- Spring MVC: How to store € character?
- Getting question mark instead accented letter using spring MVC 3
I think that org.springframework.web.filter.CharacterEncodingFilter should help here. Try it with and without your encodeURI(params)
来源:https://stackoverflow.com/questions/22658988/problems-with-passing-euro-sign-as-url-parameter