1

I have a JSP / Servlet page with JQgrid (ver 4.1.2). In this grid when there are any accent character it does not show those characters. The grid gets its input in JSON format.

I verified that the accent characters are written to the response object. Using firebug I saw that the JSON response received by the grid does not have the accent characters. The request header information is

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Type    text/html; charset=UTF-8
Cookie  XXXX
Host    XXXX
Referer http://XXXXXXXX
User-Agent  Mozilla/5.0 (Windows NT 5.2; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
X-Requested-With    XMLHttpRequest

And the response header is

Content-Type    text/html
Date    Tue, 25 Nov 2014 10:42:29 GMT
Server  Apache-Coyote/1.1
Transfer-Encoding   chunked

I also set the response header's Content-Type to application/json but also the received JSON string does not show the accent character.

response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

When I googled this issue it was mentioned to use autoencode:true and ajaxGridOptions: { contentType: 'text/html; charset=UTF-8' } and formatter:null. Also it was mentioned to add <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">. I have also added <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

But still the accent characters are not shown. Can any one let me know what I am missing?

Test codes: Servlet JQGridDemo code

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@SuppressWarnings("serial")
public class JQGridDemo extends HttpServlet {

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    String action = request.getParameter("action");
    String page = "1";
    String totalPages = "2";
    String totalCount = "15";
    String[] rowId = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "11", "12", "13", "14", "15" };
    String[] InvNo = { "101", "102", "103", "104", "105", "106", "107",
            "108", "109", "110", "111", "112", "113", "114", "115" };
    String[] DateRec = { "03/30/12", "03/31/12", "04/01/12",
            "04/02/12", "04/03/12", "04/04/12", "04/05/12",
            "04/06/12", "04/07/12", "04/08/12", "04/09/12",
            "04/10/12", "04/11/12", "04/12/12", "04/13/12" };
    String[] AmountRec = { "11000", "12000", "13000", "14000", "15000",
            "16000", "17000", "18000", "19000", "20000", "21000", "22000",
            "23000", "24000", "25000" };
    String[] TaxRec = { "1000", "1000", "1000", "1000", "1000", "1000",
            "1000", "1000", "1000", "1000", "1000", "1000", "1000", "1000",
            "1000" };
    String[] TotalRec = { "12000", "13000", "14000", "15000", "16000",
            "17000", "18000", "19000", "20000", "21000", "22000", "23000",
            "24000", "25000", "26000" };
    String[] NotesRec = { "Mineta San José", "Invoice 2", "Invoice 3",
            "Invoice 4", "Mineta San José", "Invoice 6", "Invoice 7",
            "Invoice 8", "Invoice 9", "Invoice 10", "Invoice 11",
            "Invoice 12", "Invoice 13", "Invoice 14", "Invoice 15" };

    response.setContentType("application/json; charset=UTF-8");
    response.setCharacterEncoding("utf-8");
    try {
        if (action.equals("fetchDataJSON")){
            response.setContentType("text/json; charset=UTF-8");

            JSONArray cellarray = new JSONArray();
            JSONObject responsedata = new JSONObject();
            responsedata.put("page", page);
            responsedata.put("total", totalPages);
            responsedata.put("records", totalCount);

            for(int record = 0; record < rowId.length; record++){
                JSONArray cell = new JSONArray();
                JSONObject cellobj = new JSONObject();
                cellobj.put("id", record + "");
                cell.add(InvNo[record]);
                cell.add(DateRec[record]);
                cell.add(AmountRec[record]);
                cell.add(TaxRec[record]);
                cell.add(TotalRec[record]);
                cell.add(NotesRec[record]);
                cellobj.put("cell", cell);
                cellarray.add(cellobj);
                /*cellobj.clear();
                cell.clear();*/
            }
            responsedata.put("rows", cellarray);
            out.write(responsedata.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        out.close();
    }
}

// Process the doGet request
@Override
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

// Process the doPost request
@Override
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

}

Test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="application/json; charset=UTF-8">
<title>Home</title>
<script type="text/javascript">
jQuery(document).ready(function(){ 
  jQuery("#list").jqGrid({
    url:'<%=request.getContextPath()%>/JQGridDemo?q=1&action=fetchDataJSON',
    datatype: 'JSON',
    autoencode:true,
    mtype: 'GET',
    colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
    colModel :[ 
      {name:'invid',index:'invid',width:55,
          sortable:false,editable:true,hidden:false,
          editoptions: {disabled: false},
          editrules:{edithidden:true,custom:false}}, 
      {name:'invdate', index:'invdate', width:160,
         sortable:false,editable:true,hidden:false,
         editrules :{edithidden:false,required:false,date:true},
         editoptions :{dataInit:function(element){$(element).datepicker(
                       {dateFormat:'mm/dd/yy'});}},
         formatoptions: {newformat:'m/d/Y'},
         formoptions:{elmprefix:' '}}, 
      {name:'amount',index:'amount',width:80,align:'right',
          sortable:false,editable:true,hidden:false,
          editoptions: {disabled: false},
          editrules:{edithidden:true,custom:false},
          formoptions:{elmprefix:'*'}}, 
      {name:'tax',index:'tax',width:80,align:'right',
          sortable:false,editable:true,hidden:false,
          editoptions: {disabled: false},
          editrules:{edithidden:true,custom:false}}, 
      {name:'total',index:'total',width:80,align:'right',
          sortable:false,editable:true,hidden:false,
          editoptions: {disabled: false},
          editrules:{edithidden:true,custom:false}}, 
      {name:'note', index:'note', width:150, 
          sortable:false,editable:true,hidden:false,
          editoptions: {disabled: false},
          editrules:{edithidden:true,custom:false},
          autoencode: false} 
    ],
    pager: '#pager1',
    rowNum:5,
    height:'auto',
    loadonce:true,
    width:'100%',
    gridview: true,
    autowidth:true,
    shrinkToFit:false,
    rowList:[5,10,15,20,25,30],
    sortname: 'invid',
    sortorder: 'asc',
    viewrecords: true,
    editurl:'<%=request.getContextPath()%>/JQGridDemo?q=1&action=editDataJSON',
    caption: 'My first grid',
    loadError: function (jqXHR, textStatus, errorThrown) {
        alert('HTTP status code: ' + jqXHR.status + '\n' +
              'textStatus: ' + textStatus + '\n' +
              'errorThrown: ' + errorThrown);
        alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
    }
  });
  $("#list").jqGrid('navGrid',"#pager1",{add:true,edit:true,del:true,search:true,refresh:true,
        beforeRefresh: function(){
          $("#list").jqGrid('setGridParam',{datatype:'json'}).trigger('reloadGrid', [{page:1}]);
        }},
        {},{},{},{
           closeAfterSearch: true,
           modal: true
        });
});
</script>
</head>
<body>
<table id="list">
</table>
<div id="pager1"></div>
</body>

I have tested this code with json-lib-2.4-jdk15.jar and json-simple.jar JSON library. The accent characters are not shown in the JQGrid

Natraj
  • 397
  • 4
  • 9
  • 35
  • You wrote yourself: "Using firebug I saw that the JSON response received by the grid does not have the accent characters." So the problem which you have isn't depend on jqGrid. It's in the **server code** which returns JSON data. You should post more details about the code. It should include something like `response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.getWriter().write(pureJsonData);` or another depend on technology used on the server. – Oleg Dec 05 '14 at 08:02
  • On further analyzing the firebug that in the console tab the accent characters are not shown properly but, in the net tab the characters are shown properly. I also debugged the server code and found that till the data is written to the response object the accent characters are shown properly. Once it is sent to the UI and received in the UI the characters are getting lost. I also added `URIEncoding="UTF-8" useBodyEncodingForURI = "true"` in tomcats server.xml file. Then also the characters are not shown. – Natraj Dec 05 '14 at 13:44
  • You should opens the HTML page which contains jqGid in web browser and examine the header part of it. You should append your code with the information starting with ` `. The head have to contain `` or some close line which exact syntax depend on ` ` which you use. – Oleg Dec 05 '14 at 14:04
  • `@Oleg` : In my JSP page I have following lines of code `<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>` Afterwards this line ` ` and In `` tag `` – Natraj Dec 08 '14 at 10:29
  • Below is the rendered page source ` Zumigo Admin UI ` – Natraj Dec 08 '14 at 10:31
  • It's a little strange that you use `` together with non-closed tags `` instead of `` and ` ` and then `` and so on or you use HTML and then use ``. I recommend you to validate your page on http://validator.w3.org/ – Oleg Dec 08 '14 at 10:55
  • I have added the code to reproduce the issue in my question – Natraj Dec 09 '14 at 13:31
  • I'm not Java developer and the server code can't help me. You should use Developer Tools of Chrome/Firefox/Internet Explorer to examine HTTP traffic from the server. Could you include the JSON response which returns your server in the text of your question? Nevertheless **I wrote you in my first comment that correct `ContentType` should be `"application/json"`, but you used `text/html; charset=UTF-8` or `text/json; charset=UTF-8`**. I don't see `response.setCharacterEncoding("utf-8");` in the code too. `Test.jsp` don't contains any HTML markup. **just reread my first comment please.** – Oleg Dec 09 '14 at 13:57
  • 1
    You use `out.write` and very strange `System.out.println("cellarray = " + cellarray);` instead of setting first `ContentType` and `CharacterEncoding` of `response`, getting the writer by `response.getWriter()` and usage it to write the results. (see my first comment). I want to stress that you can't append JSON response with some debug data like `System.out.println("cellarray = " + cellarray);`. In any way I recommend you to add `loadError` callback to jqGrid (see [the old answer](http://stackoverflow.com/a/6969114/315935)) – Oleg Dec 09 '14 at 14:04
  • As per your comments I have tried with different content type `text/html`, `text/plain`, `application/json`. Also I added `loadError` but the alert message is not shown. System.out.println is equivalent to Console.Write. I have updated the code in my question. – Natraj Dec 09 '14 at 14:26
  • I cant change the content type in the very first line in the jsp code. If I change the content type then it will show whole page source instead of rendering it. – Natraj Dec 09 '14 at 14:29
  • You don't need to try different content type. It should be `application/json` see [the answer](http://stackoverflow.com/a/477819/315935) or RFC 4627. contentType of HTML file is OK. The current Servlet code JQGridDemo contains **string constants in Java code**. Do you verified that the text file are saved with UTF-8 encoding? To be sure just add some clear Unicode text inside like "Олег" (Oleg) inside of some `NotesRec` item. Just save the file and open it one more time to see the same texts. Do you tried Developer Tools? Which JSON data you see in the HTTP response? Could you insert the data? – Oleg Dec 09 '14 at 14:34
  • Your code still use `response.setContentType("text/json; charset=UTF-8");` for `fetchDataJSON` action which expect `response.setContentType("application/json"); response.setCharacterEncoding("utf-8");` The rule is very easy: if the returned data are pure JSON response it should contain `application/json` Content Type and the text must be utf-8 encoded. Your *HTML code* can have *another Content Type*: `text/html`. – Oleg Dec 09 '14 at 14:40
  • Here is the JSON response `{"total":"2","page":"1","records":"15","rows":[{"id":"0","cell":["101","03\/30\/12","11000","1000","12000","Mineta San Jos?"]},{"id":"1","cell":["102","03\/31\/12","12000","1000","13000","Invoice 2"]},{"id":"2","cell":["103","04\/01\/12","13000","1000","14000","Invoice 3"]},{"id":"3","cell":["104","04\/02\/12","14000","1000","15000","Invoice 4"]}]}`. I verified that the files are saved as UTF-8. If I directly add `Mineta San José` string to the JSP page it prints properly but not in the JQGrid. – Natraj Dec 09 '14 at 14:46
  • First of all you should validate the JSON data returned from the server [here](http://jsonlint.org/) for example. Then you can save the data as a file like [this one](http://www.ok-soft-gmbh.com/jqGrid/Natraj.json) and place it on the same location as HTML/jsp file. You can use `utl` with data from the file. Like [the demo](http://www.ok-soft-gmbh.com/jqGrid/Natraj.htm) do. You can see that all JSON data will be correctly displayed. So I think that your server returns still wrong encoded JSON data (not as utf-8). You can verify the HTTP body (JSON) from my demo with youth. – Oleg Dec 09 '14 at 15:07

1 Answers1

0

As @Oleg told that the problem was not in the way the JSON data was prepared or in the JQGrid. But the problem was in the way the response writer object was used. In my code I was getting the Response PrintWriter object once and then using it in my code based on requested object. I was setting the ContentType & CharacterEncoding before sending the data. But that is not the proper way. The ContentType & CharacterEncoding on the Response PrintWriter object has to be set before it is acquired. Once I did that now the accented characters are properly shown in JQGrid.

Natraj
  • 397
  • 4
  • 9
  • 35