0

My servlet is giving me a CSV file with chars like 'é', 'á' or 'õ'. When I open the servlet via browser, it works fine. But when the information get to my page, it's all �.

I tried changing the coding in the actual database, but it didn't work.

console.log(csv), where csv is the information from the servlet, and it's also full of '�'s.

I'm not even sure where the problem is, because the actual page has several latin characters which are presented properly.

I'm using Microsoft SQL '08, tried both nvarchar and varchar fields.

Johnny Bigoode
  • 578
  • 10
  • 31
  • does serializing data helps you. – Ankur Singhal Oct 16 '14 at 15:15
  • Did you set up the encoding of your webpage ? – Superdrac Oct 16 '14 at 15:15
  • 1
    You most likely have a character encoding problem. Which means, there could be a lot going on that is causing your problem. Setting the page encoding as @Superdrac suggests might help. But my recommendation is to check the character encoding at every step and make sure everything is lined up correctly. Lower probability at this point, but you could also be dealing with microsoft special characters. – hooknc Oct 16 '14 at 15:18
  • OK. How can I check the encoding from the servlet? – Johnny Bigoode Oct 16 '14 at 16:48
  • There was no encoding, I just had to check the actual Java code. On a side note, Ajax assigns a specific charset when there's no header. – Johnny Bigoode Oct 16 '14 at 17:02

3 Answers3

2

The server has to be instructed to use UTF-8 to decode the JSP output. This can on a per-JSP basis be done by

<%@page pageEncoding="UTF-8" %>

or on an application-wide basis by

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

refer here for more.

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • The pages (and I guess the server) are using ISO-8859-1 code: ´<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>´ I tried changing the encoding to UTF-8, but nothing has changed. – Johnny Bigoode Oct 16 '14 at 16:39
1

What Ankur posted seems to fix most problems, but I would still have issues from time-to-time with encoding. I really hope you don't have to do this, but I was forced to make a little decode function to replace problematic characters (by using their '\u' code) with web-safe ones.

0

It seems that the encoding for the page is actually ISO-8859-1. While searching around stackoverflow for the terms mentioned by users I found this:

jQuery $.get() charset of reply when no header is set?

The error happens because there's no header whatever Ajax is getting, setting this up before the $.get command solves this:

$.ajaxSetup({
    'beforeSend' : function(xhr) {
        xhr.overrideMimeType('text/html; charset=ISO-8859-1');
    },
});

Now, why is the server not configured for a more practical charset, I don't know.

Community
  • 1
  • 1
Johnny Bigoode
  • 578
  • 10
  • 31