1 Reply Latest reply: Feb 6, 2014 12:50 AM by gimbal2 RSS

Encoding issue with Servets

865304 Newbie
Currently Being Moderated

I am facing a strange issue to display non-english characters in a servlet. I have an helper class produces non-english characters as below

 

public String getContent()

String content = "કેન્દ્ર સરકારે ચૂંટણી નજીક આવતાં સામાન્ય જનતાને ";

char[] newch = content.toCharArray();

  byte[] bytes = Charset.forName("UTF-8").encode(CharBuffer.wrap(newch)).array();

  String text1 = new String(bytes);

System.out.println("Non-english content :"+text1);

return text1;

}

I can see non-english text on console when run standalone but when called from servlet it shows some garbage e.g.

ArticleLocalProcessor localProcessor = new ArticleLocalProcessor();

  String jsonString = localProcessor.parseNeko();

  resp.setHeader("Content-Type", "text/plain; charset=ISO-8859-1");

  resp.setCharacterEncoding("ISO-8859-1");

  PrintWriter out = resp.getWriter();

out.println(jsonString);

 

Output: કેન્દ્ર સરકારે ચૂંટણી

 

Any help

  • 1. Re: Encoding issue with Servets
    gimbal2 Guru
    Currently Being Moderated

    So perhaps if your characters turn into garbage, you're using the wrong character set then.

     

    I think the only "problem" here is that you labelled this is strange, don't ever do that because it is a flight response. You need to fight - find the mistake you're making.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points