On Wed, 12 Dec 2001, yilmaz wrote: > Date: Wed, 12 Dec 2001 11:16:00 +0800 > From: yilmaz <[EMAIL PROTECTED]> > Reply-To: Tomcat Users List <[EMAIL PROTECTED]> > To: Tomcat Users List <[EMAIL PROTECTED]> > Subject: charset problem in java beans > > hi everybody, > i have posted a few emails a while ago about the big5 charset support in JSP > pages. > Fortunately the problem is solved, i appreciae those who helped me. > Now, i have a new version of this problem , and it seems it is a little bit > more > complicated than the previous one. The problem is: > i am using a java bean in my jsp page to hold current items list of > customers in their shopping cart. > i am using hashtables in my java bean. Now i faced the same charset problem > here. > except the item names (in big5) which are retreived from the java bean, > everything works fine. > the chinese item names are displayed garbled. > Any one out there faced and solved the problem, or would like to suggest a > solution for this trouble? > by the way, i am using tomcat 4, jdk 1.3.1 on win 2000. > below is a snippet from my code : > > items=sb.getItems(); // sb is the java bean , and getItems method returns > the keys of the hashtable > while (items.hasMoreElements()) { > itemid=(String)items.nextElement(); // itemid is the key value > // getItemname(itemid) returns the correspondent > value (itemname) > product=new String(sb.getItemname(itemid).toString().getBytes(),"big5"); // > itemid is the key value used to > // retreive the > value from the hashtable > > i am loking forward to your help with smart solutions :) > thanks for taking time > cheers > yilmaz >
It sounds like you might be working too hard :-). Internally, Java keeps all String values in Unicode. When you actually write the response, it will be converted according to the character encoding you specify on the page. If you're using JSP you would put this at the top of your page: <%@ page contentType="text/html;charset=Big5" %> and then write out the values something like this: <%= sb.getItemname(itemid) %> >From a servlet, the important issue is to set the content type and character encoding *before* you get the PrintWriter: response.setContentType("text/html;charset=Big5"); PrintWriter writer = response.getWriter(); ... writer.print(sb.getItemname(itemid)); In either case, Java will perform the Unicode->Big5 conversion for you. Craig McClanahan -- To unsubscribe: <mailto:[EMAIL PROTECTED]> For additional commands: <mailto:[EMAIL PROTECTED]> Troubles with the list: <mailto:[EMAIL PROTECTED]>