Ekta Singh
Ekta Singh

Reputation: 31

AEM/CQ5 6.2 UTF-8 Character encoding

I am not able to get special characters or the characters from a different language like French. Already changed the Request Parameter from ISO-8859-1 to UTF-8. Made the changes in the Main Servlet as well.

For example : "İş hayatında ne çok engelle karşılaşıldığını, her rakamın değerini, zamanın en büyük rakip olduğunu, güvenliği elden bırakmamayı ve iyi bir iş ortağının önemini, bilen bilir. Sprinter’i ve yeni Vito’suyla, Mercedes-Benz hafif ticari araçlar kazanmak için doğdular.

Tıpkı sizin gibi."

While trying to generate a json output for this, it doesn't appear.

Please suggest a way. Thanks.

Upvotes: 1

Views: 1029

Answers (1)

VAr
VAr

Reputation: 2601

Am not sure how your implementation is, below is an example that might help you for calling a service with UTF-8 data (taken your example data)

Sample JSP that calls the Service reference with method parameter as below

<%--
  Osgi HelloService Calling component.
--%><%
%>
<%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" %>
<%@ page import="org.json.simple.JSONObject,java.util.*"%>
<%@ page import="com.mycompany.mytestservice.HelloService" %>  
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!-- getting my service reference -->
<%
com.mycompany.mytestservice.HelloService hs = sling.getService(com.mycompany.mytestservice.HelloService.class);
%>

<!-- got the string back as jobject -->
<%
JSONObject jobject = hs.getJsonObj("İş hayatında ne çok engelle karşılaşıldığını, her rakamın değerini, zamanın en büyük rakip olduğunu, güvenliği elden bırakmamayı ve iyi bir iş ortağının önemini, bilen bilir. Sprinter’i ve yeni Vito’suyla, Mercedes-Benz hafif ticari araçlar kazanmak için doğdular. Tıpkı sizin gibi."); %>

<br/>
<b>UTF string converted to JSON Object ::</b><br/>
<!-- display into page -->
<%
out.println(jobject);
%>

HelloService Interface

package com.mycompany.mytestservice;

import org.json.simple.JSONObject;

/**
 * A simple json service interface
 */
public interface HelloService {

    /**
     * @return the JSON Object of requested data
     */
    public JSONObject getJsonObj(String jsonobjp);

}

HelloServiceImpl class

package com.mycompany.mytestservice.impl;

import javax.jcr.Repository;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mycompany.mytestservice.HelloService;

/**
 * One implementation of the {@link HelloService}.
 */
@Service
@Component(metatype = false)
public class HelloServiceImpl implements HelloService {

 protected final Logger log = LoggerFactory.getLogger(this.getClass());

    public JSONObject getJsonObj(String jsonobjp){
    JSONObject jsonobj = new JSONObject();
    try { 
        jsonobj.put("testint", 30);
        jsonobj.put("myjspstring", jsonobjp); // adding to json object
        JSONArray list = new JSONArray();
        list.add("message 1");
        list.add("message 2");
        jsonobj.put("messages", list);
       log.info("*** JSON Object ***" + jsonobj);

    } 
 catch (Exception e) {
    e.printStackTrace();
}
return jsonobj; // return json object
    }

}

which finally when the jsp called resulting the following output of the JSON object json output

Note: make sure you handle the \u2019 unicode character which is not as expected output because of the JSON implementation. have more details from here

Another similar kind of example you can find how-to-set-utf-8-response-on-dopost-call

Upvotes: 1

Related Questions