1

I used all "utf-8" encoding for my jsp pages .

I used struts2 as my web framework and ran it on a tomcat.

Recently, I found that part of Chinese characters were garbled and the remainder where normal.

Plz take a look at the below pics:

enter image description here

This picture is about the output html source code. Here, the content of the data-tmpl attribute of the link must be the same as div content above.

But it was not, and there part of the content were garbled.

Here are the jsp codes:

page encoding:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

the logic code of the picture above:

<div class="float-left ${st.odd?'odd':'even'}" >
    ${template }【${signNoBracket }】
</div>
<a class="sms-editor-select-tmpl float-right" data-tmpl="${template }" data-sign="${signNoBracket }" href="###" style="display:none;">使用</a>
<p class="clear"></p>

this jsp page was include by a parent jsp page by:

<s:include value="/controls/2.0/sms-editor.jsp"></s:include>

The ref of 'template' are all the same by using EL expression.

So why could this happen? Is there any problem with tomcat output encoding?


add at 2013-11-1 0:25:02

Hi all, this days I do some experiment to find that either tomcat or struts framework lead to this problem occur.

I use jsp scripts to instead el expression, the problem still exist.

Here are some code snaps:

<%
   ValueStack vs = TagUtils.getStack(pageContext);  
   Object value = vs.findValue("template");
   System.out.println(value);
%>
<a class="sms-editor-select-tmpl float-right" data-tmpl="<%=value %>" data-sign="${signNoBracket }" href="###" style="display:none;">使用</a>

And I can see the value println in tomcat console, it is normal as we expected. So, there must be some bugs in tomcat output stream of chinese character.

the tomcat's output (it is very normal):

#收货人#您好,看到您给的中差评,抱歉让您有了不满意的购物经历。若可以我们愿意承担运费为您提供退货,希望旺旺联系我们!

I test my application on two version of tomcat, they all have this problem. One is apache-tomcat-6.0.30 and the other one is apache-tomcat-7.0.47 .

So, is there any body can help me solve this problem? Or just show me the way.


now I try jetty 9.0.6 to run my application, the problem still exist.... Who can tell me what the hell....

OTL orz !!!

jiacheo
  • 308
  • 1
  • 2
  • 12
  • Have you followed every step of http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8? – Philipp Reichart Oct 30 '13 at 03:46
  • Where does the garbled data come from? Are you sure it's not already garbled when you put it into the template? – Philipp Reichart Oct 30 '13 at 03:47
  • @PhilippReichart the data is not garbled because the content of the div above the link is also come from the same template by el expression ${template }. – jiacheo Oct 30 '13 at 04:02
  • possible duplicate of http://stackoverflow.com/questions/138948/how-to-get-utf-8-working-in-java-webapps/138950 – coding_idiot Oct 30 '13 at 06:01
  • Hi @coding_idiot, this problem here is not about encoding of url or uri, the data is read from database or some is 'hard coding' text, and the picture above shows that : I display a text by el expression twice, but one of them has some garbage characters and the other one is normal. – jiacheo Oct 30 '13 at 08:00
  • `` are you having this in your html head ? – coding_idiot Oct 30 '13 at 08:04
  • what's the behavior with other browsers ? – coding_idiot Oct 30 '13 at 08:04
  • @coding_idiot I have use a common top header page , all include and it behave all the same on all browser I have tested. Chrome, Firefox, IE6~10 , I think this is not the problem of browser, but may be some problem with tomcat or struts2 framework, but I don't know how to debug this problem. – jiacheo Oct 30 '13 at 08:27
  • And another thing I must mention is that this problem is not occur to all user who have login my website, just a little of them has this problem. – jiacheo Oct 30 '13 at 08:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40238/discussion-between-coding-idiot-and-jiacheo) – coding_idiot Oct 30 '13 at 08:34
  • Hi @PhilippReichart and coding_idiot , I add some experiment I do and the problem was still not solved. – jiacheo Oct 31 '13 at 16:42
  • Did you follow all steps of the link I posted in the first comment above? – Philipp Reichart Oct 31 '13 at 18:53
  • @PhilippReichart Not all. I do step 1,2,3,4 and 6. Excluded step 5 because I don't use any content-generation libraries . And step 2, I use the `org.springframework.web.filter.CharacterEncodingFilter` from spring 2.5.6 – jiacheo Nov 01 '13 at 04:13
  • @PhilippReichart , this problem here is not about the garbage character from uri or url, they come from the back-end, sometimes from mysqldb and sometimes are hard-coding of my program. The question most confused to me is that I print the same value twice, but one is normal, and the other one is part of garbled. – jiacheo Nov 01 '13 at 04:18

1 Answers1

0

Try this on your tomcat's server.xml or context.xml may help:

<Connector port="8080" ... useBodyEncodingForURI="true"...
yskkin
  • 854
  • 7
  • 24