8

I have some problem with UTF-8 in java servlet file. When I get the parameter value in the URL, I have some problem with UTF-8 characters. It does not display properly Japanese Characters.

Jsp header already has

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

I added the URIEncoding setting in the connector to UTF-8 in server.xml.

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

I wrote the code as the following in jsp.

<s:textfield key="txt_name" name="txt_name" id="txt_name"
maxlength="64"></s:textfield>

<a href="javascript:showModalWindow('PopUpFile!init.action?<%=Common.PASSWORD%>=<%=Common.encript(ID, Code)%>','',940,650);">
<s:property value="PopUp Link" />
</a>

<script>
    function showModalWindow(x_URL, x_ARG, x_WIDTH, x_HEIGHT) {
        var x_OPT = "dialogHeight: " + x_HEIGHT + "px; " + "dialogWidth: "
                + x_WIDTH + "px; "
                + "edge: Raised; center: Yes; resizable: Yes; status: Yes;";
        x_URL += "&name="+document.getElementById("txt_name").value;
        var retValue = window.showModalDialog(x_URL, x_ARG, x_OPT);
        if (retValue != null) {
            document.forms.frm.action = "ParentFile!getUser.action";
            document.forms.frm.submit();
        }
    }
</script>

And then, I wrote the code as the following in java servlet.

if(g_request.getParameter("name") != null){
    g_session.setAttribute(NAME, g_request.getParameter("name"));
}

I also tested with request.setCharacterEncoding() method in java servlet but it doesn't really work. Although I tried many ways from answers of the other people's problem related with character encoding in servlet in stackoverflow, I can't solve my problem until.

What can I do to display correctly the character encoding? Thanks in advance.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Sandar Min Aye
  • 499
  • 6
  • 15
  • 28
  • Possible duplicate of [How do I correctly decode unicode parameters passed to a servlet](https://stackoverflow.com/questions/469874/how-do-i-correctly-decode-unicode-parameters-passed-to-a-servlet) – Vadzim Sep 06 '17 at 10:09

4 Answers4

13

Most servers, including Apache Tomcat server, are configured to parameter encoding with ISO-8859-1 by default. I think you won't change this unless you have a private dedicated server instance. So, the programmer's technique is to encode/decode those parameters manually. Because you are using javascript, there's encodeURI() or encodeURIComponent() built-in function. See How to encode a URL in JavaScript. The code should change

x_URL += "&name="+encodeURI(document.getElementById("txt_name").value);

in the Java use the URLDecoder to decode parameter back.

java.net.URLDecoder.decode(((String[])request.getParameterMap().get("name"))[0], "UTF-8"));

Note, if you are using Struts2 dispatcher result type then you don't need to decode parameters in the query string. Those parameters are parsed via UrlHelper.

However, I don't remember when I decode those parameters are automatically decoded in Struts2.

As a rule you should know that if you pass parameters in the URL they should be URL encoded. If you submit the form there's no need to do it because the form is x-www-form-urlencoded, see 17.13.4 Form content types.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    Thanks @Roman C, you saved my life again ! :) I'm just a novice in struts 2 and also in javascript. You always explain me details when you answer my problem. So, I got many experiences from you. Thank you So much !!! – Sandar Min Aye Sep 30 '13 at 10:22
11

Are you testing the output using System.out.println? If so, it may be that it is not configured for UTF-8.

See this: Can not send special characters (UTF-8) from JSP to Servlet: question marks displayed

Also make sure that you run request.setCharacterEncoding("UTF-8") BEFORE reading the parameters.

Community
  • 1
  • 1
stepanian
  • 11,373
  • 8
  • 43
  • 63
  • 1
    It is already configured for UTF-8, now I tested with SOP, it is displayed as "���j�b�gC1". And also tried with `request.setCharacterEncoding("UTF-8")` but has no luck until. :( – Sandar Min Aye Sep 30 '13 at 06:25
  • 6
    request.setCharacterEncoding("UTF-8") solve my problem, thanks. – erhun Jan 12 '15 at 17:49
4

I have similar error and solve it use Servlet filter if you are using Spring, just add this definition to web.xml

<!-- CharacterEncodingFilter / UTF-8 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>

        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

if you are not using Spring write a servlet filter like :

protected void doFilterInternal(HttpServletRequest request,
            HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        //Set character encoding as UTF-8
        request.setCharacterEncoding("UTF-8");
        filterChain.doFilter(request, response);
    }
erhun
  • 3,549
  • 2
  • 35
  • 44
  • Important: make sure this filter is carried out FIRST, before any others, otherwise it won't work – f.khantsis Apr 13 '16 at 12:21
  • @doom777 I didn't face a problem like that, i may test it to define a new filter before encodingFilter, below it is working fine but web.xml only has one filter now. https://github.com/muzir/azorka.web/blob/master/src/main/webapp/WEB-INF/web.xml – erhun Apr 13 '16 at 13:52
  • Well, basically, if the parameters are accessed before the CharEncoding is set, you're lost. You have to make sure that no code accesses the request parameters before the CharacterEncodingFilter – f.khantsis Apr 13 '16 at 14:19
1

I would like to escale a comment from f.khantsis to an answer, because it turned out to be important in my case.

Important: make sure this filter is carried out FIRST, before any others, otherwise it won't work.

We had a correct encoding setting in our servlet filter:

    request.setCharacterEncoding("UTF-8");

But when I put a breakpoint in request.getParameter it showed, that we are reading parameters before setting the encoding. It was happening in another filter (csrf). At this moment the encoding was frozen and setting it further was ineffective. This may be server dependent but in our case (Websphere) it was the case.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66