Click here to Skip to main content
12,818,964 members (27,614 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 13 Mar 2014

JSP GSON Encoding Problem

, 13 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
GSON does not properly render non-English letters while converting Java object to json string in JSP.


GSON does not properly render non-English letters while converting Java object to json string.

Right from database to browser, all data is properly treated as UTF-8, but when GSON took place I saw only question marks.

I found solutions that did not work for me, so I've decided to post my solution.

Working Code Example

I'll show the entire doPost method. It is redundant but you'll be able to see the whole picture.

protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setCharacterEncoding("UTF8"); // this line solves the problem

    PrintWriter writer = response.getWriter();

    Object data = retrieveData();

    Gson gson = new GsonBuilder().create();
    gson.toJson(data, writer); // places json string right into the response

At first, I think that response.getWriter() returns already UTF-8-oriented PrintWriter. But you have to specify it explicitly via response.setCharacterEncoding("UTF8").

Hope this helps!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Roman Zinnatov
Software Developer (Junior)
Russian Federation Russian Federation
Software Developer

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 13 Mar 2014
Article Copyright 2014 by Roman Zinnatov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid