Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1081491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:10:31+00:00 2026-05-16T22:10:31+00:00

My Servlet just won’t use UTF-8 for JSON responses. MyServlet.java : public class MyServlet

  • 0

My Servlet just won’t use UTF-8 for JSON responses.

MyServlet.java:

public class MyServlet extends HttpServlet {

  protected void doPost(HttpServletRequest req, HttpServletResponse res) throws Exception {

    PrintWriter writer = res.getWriter();

    res.setCharacterEncoding("UTF-8");
    res.setContentType("application/json; charset=UTF-8");

    writer.print(getSomeJson());
  }
}

But special characters aren’t showing up, and when I check the headers that I’m getting back in Firebug, I see Content-Type: application/json;charset=ISO-8859-1.

I did a grep -ri iso . in my Servlet directory, and came up with nothing, so nowhere am I explicitly setting the type to ISO-8859-1.

I should also specify that I’m running this on Tomcat 7 in Eclipse with a J2EE target as a development environment, with Solaris 10 and whatever they call their web server environment (somebody else admins this) as the production environment, and the behavior is the same.

I’ve also confirmed that the request submitted is UTF-8, and only the response is ISO-8859-1.

Update

I have amended the code to reflect that I am calling PrintWriter before I set the character encoding. I omitted this from my original example, and now I realize that this was the source of my problem. I read here that you have to set character encoding before you call HttpServletResponse.getWriter(), or getWriter will set it to ISO-8859-1 for you.

This was my problem. So the above example should be adjusted to

public class MyServlet extends HttpServlet {

  protected void doPost(HttpServletRequest req, HttpServletResponse res) throws Exception {

    res.setCharacterEncoding("UTF-8");
    res.setContentType("application/json");

    PrintWriter writer = res.getWriter();
    writer.print(getSomeJson());
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:10:32+00:00Added an answer on May 16, 2026 at 10:10 pm

    Once the encoding is set for a response, it cannot be changed.

    The easiest way to force UTF-8 is to create your own filter which is the first to peek at the response and set the encoding.

    Take a look at how Spring 3.0 does this. Even if you can’t use Spring in your project, maybe you can get some inspiration (make sure your company policy allows you to get inspiration from open source licenses).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have web application written in java using Eclipse. It has just one servlet
I'd like to use the URL fetch service for app engine (java). I'm just
I initially just had a Java Servlet that I needed to unit test. I
<servlet> <servlet-name>Hello</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/HelloWorld.do</url-pattern> </servlet-mapping> Why do we use url-pattern inside
tomcat 7.0.16 Java 1.6.0_22 CentOS 5.6 I just switched the web.xml to servlet 3.0
I just got a servlet class working in Eclipse. I was testing it by
I just downloaded from: http://java.sun.com/products/servlet/download.html the Java(TM) Servlet API Specification Interface Classes 2.3 Inside
I've just created my first Java EE servlet using Hibernate. What I don't know
Instead of writing too many sql statements in each servlet I just want to
I am trying just to run a servlet on my local Tomcat with Eclipse.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.