1

I use the following jsp to search some text:

<%@page session="false" contentType="text/html; charset=utf-8" %>
..
...
<form method="post"  action="searchPageAction.html">
    <input type="text" name="search_value"  value="ö-Test"/>
</form>

...

the Servlet looks like this:

public class SearchServlet {
private static final String SEARCH_VALUE = "search_value";

@Override
protected final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException {
    try {
        request.setCharacterEncoding("UTF-8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }
    String searchValue = request.getParameter(SEARCH_VALUE);
    String redirectPath = "/content/vrbp/de/tools/suchergebnis.html?q="+searchValue+"&_charset_=UTF-8";
    try {
        response.setCharacterEncoding("UTF-8");
        response.sendRedirect(redirectPath);
    } catch (IOException e) {

    } catch (IllegalStateException e) {
       }
}
}

Now when I search for example for "ö-Test", The following link is written in the browser:

http://www.testproject:4502/content/myProject/searchResult.html?q=%F6-Test&charset=UTF-8

I have expected:

http://www.testproject:4502/content/myProject/searchResult.html?q=ö-Test&charset=UTF-8

and the text ö-Test ist printet in searchResult.html as &#65533;-Test. How to encode correct in UTF-8? What did I do wrong?

Ronald
  • 2,721
  • 8
  • 33
  • 44

4 Answers4

3

You need to set the default parameter encoding, which can be configured via the Sling Main Servlet settings, i.e —

http://localhost:4502/system/console/configMgr/org.apache.sling.engine.impl.SlingMainServlet

The last option, sling.default.parameter.encoding should be set to UTF-8, or else it will fall back to the default, as specified in the help text:

The default request parameter encoding used to decode request parameters into strings. If this property is not set the default encoding is "ISO-8859-1" as mandated by the Servlet API spec. This default encoding is used if the "charset" request parameter is not set to another (supported) character encoding. Applications being sure to always use the same encoding (e.g. UTF-8) can set this default here and may omit the "charset" request parameter.

To avoid doing this manually in the Felix console in each environment, you can add a sling:OsgiConfig node with these settings in your codebase, which will then configure this setting when your code is deployed.

anotherdave
  • 6,656
  • 4
  • 34
  • 65
  • Thank you for your answer. I have set the option sling.default.parameter.encoding to UTF-8 but nothing changes :( – Ronald Nov 11 '14 at 14:51
3

If the parameter is not showing up correctly in the query string, then client side code may not be setting the encoding correctly. The properly URL-encoded value for ö-Test would be %C3%B6-Test so that is what you would want to see in the query string. Once you get that showing up correctly, then the server-side default encoding will come into play. You want to see this when you search for that example:

http://www.testproject:4502/content/myProject/searchResult.html?q=%C3%B6-Test&charset=UTF-8

When the form loads in a browser, what encoding is the browser detecting and choosing to use to render the page? You may need to add an appropriate meta tag:

HTML 4.01: <meta http-equiv="content-type" content="text/html; charset=UTF-8"> HTML5: <meta charset="UTF-8">

Or try <%@ page pageEncoding="utf-8" %>

As an example, I tried a form with <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> The resulting query string was %F6-Test But when I used <meta http-equiv="content-type" content="text/html; charset=UTF-8"> the resulting query string was %C3%B6-Test So that seems to indicate the encoding in the browser is not correct.

Also check out this question: $_POST will convert from utf-8 to ä ö ü etc It may not be directly relevant, but it lists some of the factors to consider.

Community
  • 1
  • 1
Shawn
  • 8,374
  • 5
  • 37
  • 60
1

This solve my Problem

searchValue= URLEncoder.encode(searchValue, "UTF-8");
Ronald
  • 2,721
  • 8
  • 33
  • 44
0

This worked for me:

String(request.getParameter("q").getBytes("ISO-8859-1"))
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 1
    While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Kevin Brown-Silva Mar 03 '15 at 01:29