i was trying not to get aneurysym because of this unicode stuff and i have been pulling my hair out trying to figure out what the heck is going on..... why the japanese character i have typed in on my jsp gets saved as some mangled value on oracle.... and why correct unicode values on oracle doesn't get displayed correctly on my jsp...... i would come home seething with fury because of frustration with a case of migraine on the side....... i tried posting on forums and i was mocked at because i don't know how stuff are called but i survived.... i'm posting my solution here to document this....... i was having difficulty googling the answer because apparently my search queries are wrong but i got lucky when someone on the forum i posted my question pointed me to this link:
http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/ so you need to do that....forget the database part.... i was yelled at at work when i asked what our database character set is....... and i was mocked at that if i need to ask that then it means i'm not doing stuff correctly (hmpf! fine!)...... so again, ignore the "database settings"...... as long as you know that your database is configured to be unicode-friendly your good..... then after some playing around with my codes and comparing why the other jsp is behaving correctly and the other is not i realized the other is using the "get" method (<form method="get"...> ) which is doing what i want it to do while my other jsp is using "post" which is what's screwing my inputs..... i didn't know that method "post"'s default encoding is ISO-8859-1....... excellent! i found the culprit why my japanese characters values are getting mangled...... by the way.....for the "get" to work properly you need to add this:
URIEncoding="UTF-8" on your server.xml so it would be something like: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/> ok.....back to "post" and "get"...... in the web land we don't use "get".....(if you've come this far that means you're a programmer and you were interested to know how i fixed my problem which also means you already know why "get" is a no-no)....... so i need to find a way to make the "post" to work like the "get" and so it's google time again and somewhere between searching for struts and tomcat encoding and finding the nearest papa john's pizza i found this:
http://wiki.apache.org/tomcat/FAQ/CharacterEncoding you might want to pay attention on this part: How do I change how POST parameters are interpreted? so what i did was: 1. i located that file SetCharacterEncodingFilter.java which, as the document says is in: tomcatdir/webapps/examples/WEB-INF/classes/filters/ 2. copied the contents of the whole /filters directory and dropped it in my own /webapps directory note: although actually you just need the SetCharacterEncodingFilter.xxxxx 3. and then i opened my web.xml and added this:
<filter> <filter-name>Set Character Encoding</filter-name> <filter-class>com.myclasses.filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> add those lines before any of your <servlet> tags...atleast that's where i put it... there's a shortcut...in your action or conroller classes make sure you set the encoding to what you want it to be, like in my case it's UTF-8... so somewhere in your controller class there's a line of code that looks exactly like this..
request.setCharacterEncoding("UTF-8");
for some reason that shortcut didn't work for me and i'm sure i'm screwing things up somewhere in my code, but being the lazy ass that i am and i had enough of this unicode stuff that if someone talks to me about this i am going to scream, seriously.... haha...anyway, i don't want to debug anymore so i just did that <filter> thingie.... that's it! that solves my from browser to database problem.... now, how about the from database to browser problem? easy.... locate all your .jsp and add this line right on the very top... <%@ page language="java" contentType="text/html; charset=UTF-8" %> this actually should be the easiest part of my problem and the reason why my browser still keeps displaying the wrong values is because i put that line of code on the wrong jsp..... that happens you know, specially when you didnt realize that the jsp you are looking at is a baby jsp meaning it was called by the mother jsp (sorry, i know, i have a weird way of calling stuff) using this line:
<c:import url=mommy_calling_baby.jsp/> so make sure you put that line of code on the mother jsp...
so there you have it.... if you were having the same problem and this tiny notes of mine worked for you and s olved your problem and you know where i'm at i just want a tall java chip frap from starbucks, tee hee hee...
oh, by the way, i'm using tomcat 6, oracle 11g and java 1.5 and struts p.s.. now if you're shaking your head and mumbling to yourself saying i'm stupid not to know this and you know how to do this even while you're sleeping, then good for you....now, go away! |