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
- Get to display non-ascii characters (e.g. ç,ğ,ö,ş,ı, etc) in the jsp file when they are typed directly inside the jsp.
- Get to display these characters when read from an application resources file (for example StripesResources.properties 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 java.io.IOException;
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 {
req.setCharacterEncoding(encoding);
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:
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.yourpackagestructurehere.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
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 ApplicationResources.properties (or StripesResources.properties, or xxx.properties) file in UTF-8. That should display fine right? Well wrong. It does not. But it will if you :
- Copy your ApplicationResources.properties file to something like ApplicationResources.properties.org.
- run 'native2ascii -encoding UTF-8 ApplicationResources.properties.org ApplicationResources.properties'
- 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.
Ha! it works with struts as well.
ReplyDeleteit's funny how i had it done before, which worked only with IE, not firefox. Now it works for both.
thx
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.
ReplyDeleteThanks again.
BR,
Abu.
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!
ReplyDeleteNice work, one thing you left opened are GET requests ... they are not decoded correctly by default. I wrote an article about that too.
ReplyDeleteInteresting. 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?)
ReplyDeleteWould it work with file uploads? Moreover, is it a viable solution for different browsers?
ReplyDeletethank you you saved me. i've been going crazy over this
ReplyDeletethanks you very much
ReplyDeletethanks man , u help a lot of people
ReplyDeleteVery good job!
ReplyDeleteThank you.
Thanks for the advise. This is quite different from the <valve/> config that Tomcat shows.
ReplyDeleteUnfortunately, 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.
Roger
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 é...
ReplyDeleteAny Ideas ?
Thanks! I have seen this solutions before, but you was best written guide!
ReplyDeleteI LOVE YOU MAN! This is an elegant, yet simple solution for a problem that has been bugging me for days. Thank you very much!
ReplyDeleteThank you, Cagan!
ReplyDeleteVery useful article.
Thank you for the solution. It really helped me!
ReplyDeleteThanks for the solutions
ReplyDeleteIt worked!!!
Great!!!....I was desperately looking for a solution for this problem. Now it works fine.....
ReplyDeleteThanks a lot.....
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!!:)
ReplyDeleteGood solution Thanks a lot.it helped me lot.
ReplyDeleteSüper abi ya, çok saol.
ReplyDeleteThanks 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!
ReplyDelete- Alex Yakima
Yeah man!
ReplyDeleteAll works fine yet!
Tks, tks, tks
Thanks for this article! I spent whole day changing encoding here and there and nothing really worked.
ReplyDeleteYour solution works perfectly!
Maciek
48759.....4963
ReplyDeleteIf you want to use this with jQuery.ajax() then you have to set the 'type' to 'post'.
ReplyDeleteHey ...
ReplyDeleteCan 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.
Regards
Anubhav
I think this is among the most vital information for me.
ReplyDeleteAnd 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 :: www.teenpornsexpussy.com
Appreciate the recommendation. Will try it out.
ReplyDeletemy webpage > Free Teen Porn
Spot on with this write-up, I honestly feel this website needs much more attention.
ReplyDeleteI'll probably be returning to read more, thanks for the info!
Also visit my blog post ... naked girls
It is appropriate time to make some plans for the future and it is
ReplyDeletetime 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
Outstanding story there. What occurred after? Thanks!
ReplyDeleteAlso see my page - freeporn
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
ReplyDeletebe grateful to you.
Stop by my website ... This Coed Enjoys A Slow And Easy Bop
Hi there colleagues, its impressive post about cultureand fully defined, keep it
ReplyDeleteup all the time.
My webpage > free porn
My family members every time say that I am wasting my time here at net, except I know I am getting
ReplyDeleteknow-how every day by reading such fastidious content.
my webpage: high quality beats by dr. dre headphones
This is a topic which is close to my heart... Best
ReplyDeletewishes! Where are your contact details though?
Feel free to visit my web page Scott J. Ferrell
Wow, this article is good, my sister is analyzing these kinds of things, so I am
ReplyDeletegoing to let know her.
Feel free to visit my blog post - contractors Orlando
Malaysia & Singapore & brunei finest on-line blogshop for wholesale & quantity
ReplyDeletekorean accessories, earrings, earstuds, pendant,
rings, bracelet, hair & bracelet add-ons. Offer
35 % wholesale markdown. Ship Worldwide
Feel free to surf my weblog ... www.unemployment.ohio.gov
Your style is so unique in comparison to other people I have
ReplyDeleteread 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
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.
ReplyDeleteMy web site :: Online Casino
Hi there just wanted to give you a quick heads up.
ReplyDeleteThe 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
Really when someone doesn't know afterward its up to other people that they will assist, so here it takes place.
ReplyDeletemy website ... Best Baby Monitors
Hello, this weekend is pleasant designed for me, for
ReplyDeletethe 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
Hi! I could have sworn I've been to this site before but after reading through some of the post I realized it's
ReplyDeletenew to me. Anyways, I'm definitely delighted I found it and I'll be bookmarking and
checking back frequently!
My homepage: HCG diet
Great blog here! Also your site lots up very fast!
ReplyDeleteWhat 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
Just want to say your article is as astonishing.
ReplyDeleteThe 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: http://www.69videosporno.net
You could definitely see your expertise in the
ReplyDeletework 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 ... www.sex-xxx-erotica.com
Have you ever considered publishing an e-book or guest authoring on other sites?
ReplyDeleteI 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 :: www.xpornking.info
Its not my first time to pay a quick visit this web page, i am browsing this
ReplyDeleteweb page dailly and take fastidious data from here everyday.
Feel free to surf to my blog post ... http://sexmovievault.com