Reputation: 5071
I have a problem to get a requested query param encoded in UTF-8
Request URL
localhost:8080/page?searchKey=%D9%85
Query String Parameters
searchKey:م
when tried to get this requested param by this code:
String searchKey=((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()).getParameter("searchKey");
if (!StringUtil.isEmpty(searchKey)) {
try {
System.out.println("SearchKey--------Before-------------------"+searchKey);
searchKey=URLDecoder.decode(searchKey, "UTF-8");
} catch (UnsupportedEncodingException ex) {
}
System.out.println("SearchKey---------------------------" + searchKey);
}
these two system.out
prints the following :
SearchKey--------Before-------------------�? // a square shape
SearchKey----------after-----------------�? // a square shape
So I need to catch :%D9%85 value which is request to decode it as UTF-8
so how to do that
I can fetch it from ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()).getQueryString()
but that will be a huge effort is there another way to fetch this encoded utf parmeter
HINT: Language used is Arabic
Upvotes: 2
Views: 1512
Reputation: 1108782
Request parameters as available by HttpServletRequest#getParameter()
(and its JSF flavored facade ExternalContext#getRequestParameterMap()
which you should actually be using instead of polluting your JSF beans with javax.servlet.*
artifacts, or better, the @ManagedProperty
or <f:viewParam>
, but that aside) are already decoded by the container. You don't need to pass them through URLDecoder
once again. If you want to get the raw parameters so that you can decode them yourselves, then you should indeed be using HttpServletRequest#getQueryString()
instead and be parsing the parameter name-value pairs yourself.
In your particular case, it appears that the container is using the wrong encoding to decode the query string parameter. Most containers indeed naively default to ISO-8859-1 as mandated by HTTP 1.1 which indeed isn't from this decade anymore.
So, basically, you need to explicitly tell your container to use UTF-8 instead to decode the query string parameters. Unfortunately you didn't tell anywhere which container you're using, so I can't answer that in detail. I'll assume Tomcat as a general example: you need to edit its /conf/server.xml
to add the URIEncoding
attribute to the <Connector>
element with a value of UTF-8
.
<Connector ... URIEncoding="UTF-8" />
If you're using a different container, just consult its documentation using the new keywords which you just learnt from this answer.
Upvotes: 2
Reputation: 9655
Donot decode request parameter by yourself. Container already did it.
Depends on container you used, read document to know how to configure parameter encoding.
There are 2 kind of parameter encodings. Form parameter encoding and Query string parameter encoding. For Query string parameter encoding ( Like your case ), If you using Glassfish, you can configure like this in WEB-INF/glassfish-web.xml
<glassfish-web-app>
<context-root>/webApp</context-root>
<parameter-encoding default-charset="UTF-8" />
</glassfish-web-app>
Upvotes: 1