Friday, May 05, 2006

UTF-8 Encoding fix (Tomcat, JSP, etc)

I spent a whole day trying to get non-ascii characters to display properly in my JSP pages.
To save anyone from spending similarly frustrating hours, here's the solution to get to display those characters in your JSP page.

First please read this so that you understand what the concept of encoding is.

While trying to solve my problem I collected couple of links, you can browse them here.

So my setup is as follows:
  • Tomcat Application Server (5.5.17)
  • Stripes web framework.
  • Front-end implementation JSP (using Stripes' layout functionality).
  • OS: MacOSX
Two main problems:
  1. Get to display non-ascii characters (e.g. ç,ğ,ö,ş,ı, etc) in the jsp file when they are typed directly inside the jsp.
  2. Get to display these characters when read from an application resources file (for example for Stripes).

Ok let's begin...
First make sure you save all your files (jsps, application resources files) in UTF-8 encoding. In Dreamweaver for example, Ctrl-J (or Apple-J) will bring up the window to set that.

Solution to problem 1:

I may have overkilled here, but this setup works, so you may adopt the IIWDQ ('if it works don't question') approach.

  • Place '<%@ page language="java" pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>' as the first line in 'ALL' the jsps.

    If you are using a layout manager, similar to Stripes layout, you may think 'hey I'll just put it in the layout page that way it will work for all my pages'..THINK AGAIN. IT WON'T.

    You may also say 'hey wait I have a great idea, I have this include.jsp where I declare all the taglibs, I'll place this directive in that file...All my jsps include that file, so it will work'. To that I'll say NOPE.

  • Place <meta equiv="Content-Type" content="text/html; charset=UTF-8"> under <head>. This is to give browsers an idea about the content of the page so they can display the contents properly.
  • Write an Encoding filter and make sure all your requests pass through it. Not difficult at all. Here it is:


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
private String encoding;
private FilterConfig filterConfig;

* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
public void init(FilterConfig fc) throws ServletException {
this.filterConfig = fc;
this.encoding = filterConfig.getInitParameter("encoding");

* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
chain.doFilter(req, resp);

* @see javax.servlet.Filter#destroy()
public void destroy() {


The way you let your web application know about this filter is via the web.xml file:



At this stage, if you type something along the lines of 'çanak çömlek patladı' in your jsp and run the web application, you should see it in your browser...

Are we done? Not yet. Because if you have something like <fmt:message key="username"> in your jsp and your resource properties file contains username=Kullanıcı Adı, you will end up
with something like 'Kullan?c? Ad?'...For that see:

Solution to problem 2:
I know you saved your (or, or file in UTF-8. That should display fine right? Well wrong. It does not. But it will if you :
  1. Copy your file to something like
  2. run 'native2ascii -encoding UTF-8'
  3. Deploy your files...

And ta-ta! (At least for me it was 'ta-ta' at this stage)...

Special thanks to cleverpig, mj and Rick Smith from Stripes mailing list for their help on this subject.

Ha by the way, if you are not using Stripes yet, it's time you start using it.


  1. Ha! it works with struts as well.
    it's funny how i had it done before, which worked only with IE, not firefox. Now it works for both.


  2. Hi thanks so much for this writing. I was really having problems in making this encoding stuff work in my web application. After I saw your blog and checked my application against your suggestions, I started to really get the idea what was going on and what i didn't do right. Now things working quite nicely.

    Thanks again.


  3. Omgoodness thanks to your article.. it sloved my prob in inserting and displaying in postgress! I spent the entire day till i stumble upon your site!

  4. Nice work, one thing you left opened are GET requests ... they are not decoded correctly by default. I wrote an article about that too.

  5. Interesting. All this works for me, but only if I put properties files inside a jar file in WEB-INF/lib/; if I drop .properties files in /WEB-INF/classes dir it doesn't get utf encoding right. Any ideas? (I didn't code the web.xml filter; might that solve it?)

  6. Would it work with file uploads? Moreover, is it a viable solution for different browsers?

  7. thank you you saved me. i've been going crazy over this

  8. thanks man , u help a lot of people

  9. Very good job!

    Thank you.

  10. Thanks for the advise. This is quite different from the <valve/> config that Tomcat shows.

    Unfortunately, anything that does request.setCharacterEncoding() appears to create to values for each parameter name only when a post request is made, not a get request.


  11. Hi, I had a problem with the utf-8 when I use input text on my web page, and I send the info by post method, I resive the special's characters wrong If I send é I recive é...
    Any Ideas ?

  12. Thanks! I have seen this solutions before, but you was best written guide!

  13. I LOVE YOU MAN! This is an elegant, yet simple solution for a problem that has been bugging me for days. Thank you very much!

  14. Thank you, Cagan!
    Very useful article.

  15. Thank you for the solution. It really helped me!

  16. Thanks for the solutions
    It worked!!!

  17. Great!!!....I was desperately looking for a solution for this problem. Now it works fine.....

    Thanks a lot.....

  18. Thanks man! The solution can be found on google in several places in the discussion groups etc, but it contains the answer to just part of the question. You covered it all, thank you sooooooo much!!:)

  19. Good solution Thanks a helped me lot.

  20. Süper abi ya, çok saol.

  21. Thanks man. I just fixed my problem with rendering Cyrillic symbols for tomcat under mac os x. Just applied encoding directive at the very beginning of the jsp page. This helped a lot!

    - Alex Yakima

  22. Yeah man!
    All works fine yet!

    Tks, tks, tks

  23. Thanks for this article! I spent whole day changing encoding here and there and nothing really worked.
    Your solution works perfectly!

  24. If you want to use this with jQuery.ajax() then you have to set the 'type' to 'post'.

  25. Hey ...

    Can you elaborate on why below does not work :-

    "You may also say 'hey wait I have a great idea, I have this include.jsp where I declare all the taglibs, I'll place this directive in that file...All my jsps include that file, so it will work'. To that I'll say NOPE."

    If we are confident that first line in every jsp is setting the encoding, then why not the above works.


  26. I think this is among the most vital information for me.
    And i am glad reading your article. But should remark on some general things, The website style is perfect,
    the articles is really excellent : D. Good job, cheers
    Here is my website ::

  27. Appreciate the recommendation. Will try it out.
    my webpage > Free Teen Porn

  28. Spot on with this write-up, I honestly feel this website needs much more attention.
    I'll probably be returning to read more, thanks for the info!
    Also visit my blog post ... naked girls

  29. It is appropriate time to make some plans for the future and it is
    time to be happy. I have learn this post and
    if I may I wish to suggest you some attention-grabbing things or suggestions.
    Perhaps you can write subsequent articles referring to this article.
    I desire to learn even more things about it!
    Also visit my web blog :: teen porn

  30. Outstanding story there. What occurred after? Thanks!
    Also see my page - freeporn

  31. We're a group of volunteers and starting a new scheme in our community. Your web site offered us with useful info to work on. You've performed a formidable process and our entire group will likely
    be grateful to you.
    Stop by my website ... This Coed Enjoys A Slow And Easy Bop

  32. Hi there colleagues, its impressive post about cultureand fully defined, keep it
    up all the time.
    My webpage > free porn

  33. My family members every time say that I am wasting my time here at net, except I know I am getting
    know-how every day by reading such fastidious content.
    my webpage: high quality beats by dr. dre headphones

  34. This is a topic which is close to my heart... Best
    wishes! Where are your contact details though?
    Feel free to visit my web page Scott J. Ferrell

  35. Wow, this article is good, my sister is analyzing these kinds of things, so I am
    going to let know her.
    Feel free to visit my blog post - contractors Orlando

  36. Malaysia & Singapore & brunei finest on-line blogshop for wholesale & quantity
    korean accessories, earrings, earstuds, pendant,
    rings, bracelet, hair & bracelet add-ons. Offer
    35 % wholesale markdown. Ship Worldwide
    Feel free to surf my weblog ...

  37. Your style is so unique in comparison to other people I have
    read stuff from. Many thanks for posting when you have the opportunity, Guess I'll just book mark this page.
    Feel free to visit my homepage compare wedding Insurance

  38. Hi, Neat post. There's a problem along with your website in web explorer, would test this? IE still is the marketplace leader and a big portion of people will miss your great writing due to this problem.
    My web site :: Online Casino

  39. Hi there just wanted to give you a quick heads up.
    The words in your content seem to be running off the
    screen in Chrome. I'm not sure if this is a format issue or something to do with internet browser compatibility but I thought I'd post to let you know.

    The style and design look great though! Hope you get the problem
    solved soon. Cheers

    Check out my web-site ... Miami weight loss centers

  40. Really when someone doesn't know afterward its up to other people that they will assist, so here it takes place.

    my website ... Best Baby Monitors

  41. Hello, this weekend is pleasant designed for me, for
    the reason that this moment i am reading this impressive
    informative article here at my house.

    Feel free to visit my web site ... online shopping in india

  42. Hi! I could have sworn I've been to this site before but after reading through some of the post I realized it's
    new to me. Anyways, I'm definitely delighted I found it and I'll be bookmarking and
    checking back frequently!

    My homepage: HCG diet

  43. Great blog here! Also your site lots up very fast!

    What host are you the usage of? Can I am getting your associate hyperlink in your host?
    I desire my web site loaded up as quickly
    as yours lol

    Also visit my page - HCG

  44. Just want to say your article is as astonishing.

    The clearness in your post is just cool and i can assume you are an expert on this subject.
    Fine with your permission allow me to grab your feed to keep up to date with forthcoming post.
    Thanks a million and please continue the rewarding work.

    My website:

  45. You could definitely see your expertise in the
    work you write. The world hopes for even more passionate writers such as you who are not afraid to
    say how they believe. All the time follow your heart.

    my web site ...

  46. Have you ever considered publishing an e-book or guest authoring on other sites?

    I have a blog based upon on the same topics you discuss and would really like to have you share some stories/information.
    I know my readers would enjoy your work.
    If you are even remotely interested, feel free to
    shoot me an e-mail.

    my webpage ::

  47. Its not my first time to pay a quick visit this web page, i am browsing this
    web page dailly and take fastidious data from here everyday.

    Feel free to surf to my blog post ...

  48. obviously like your website but you have to check the spelling on several of your posts.
    Several of them are rife with spelling problems and I to find it very bothersome to tell the
    truth on the other hand I'll surely come back again.

    Feel free to visit my web site: Jasmine Tame

  49. Hi there mates, its enormous article concerning educationand fully defined, keep it up all the time.

    Here is my homepage -

  50. Wow, this post is fastidious, my younger sister is analyzing such things, thus I am
    going to inform her.

    Also visit my site - Teen Sex

  51. I enjoy looking through an article that can make men and women think.
    Also, many thanks for permitting me to comment!

    Have a look at my homepage :: massage happy ending

  52. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to
    my blog that automatically tweet my newest twitter updates.
    I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

    Here is my site - sexygirlchat

  53. Nice post. I was checking continuously this blog and
    I am impressed! Very helpful information specifically the last part :) I care for such info
    a lot. I was looking for this particular information for a very long time.

    Thank you and good luck.

    Here is my web blog

  54. I am really glad to read this webpage posts which contains plenty of valuable information,
    thanks for providing these kinds of statistics.

    My web page

  55. I'm not sure exactly why but this website is loading incredibly slow for me. Is anyone else having this issue or is it a issue on my end? I'll check back later
    on and see if the problem still exists.

    my web-site -

  56. Does your site have a contact page? I'm having trouble locating it but, I'd like
    to shoot you an email. I've got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.

    Feel free to surf to my web-site ... hotgirlsexcam.Com

  57. I believe that is one of the so much vital info for me.
    And i am satisfied studying your article. However should observation on some normal things,
    The site style is great, the articles is in reality excellent : D.
    Good process, cheers

    Check out my page:

  58. Genuinely when someone doesn't be aware of afterward its up to other people that they will assist, so here it takes place.

    My website - sex xxx

  59. Hi there, i read your blog from time to time and i own
    a similar one and i was just curious if you get
    a lot of spam responses? If so how do you prevent it, any plugin or anything you can suggest?
    I get so much lately it's driving me mad so any help is very much appreciated.

    Have a look at my site - download free porn

  60. Hey, thanks for sharing this helpful post. You are right that you have saved a lot of time of all the people who gonna spent on this frustrating problem of not showing characters in JSP page. I have to made changes in a criminal lawyer boston website. Thanks once again for help.

  61. Hi, I had a similar problem sending json to a page and solved with this:


    Thanks !!

  62. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.
    I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    If anyone wants to become a Java developer learn from Java EE Online Training from India.
    or learn thru Java EE Online Training from India .
    Nowadays Java has tons of job opportunities on various vertical industry.

  63. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Best Devops Training in pune
    Best Devops Training institute in Chennai


  64. A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.

    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training