用tomcat运行web应用程序时,后台处理后的字符串,控制台打印出来是正常的,返回给页面的时候变成乱码,但是用glassfish运行又是正常的,glassfish的默认字符集是UTF-8,我将tomcat的字符集改为UTF-8,但是还是乱码,求大虾指教,感激不尽!

解决方案 »

  1.   

    返回的时候给response的设置字符集 就ok 了
      

  2.   

    web.xml中设置下试试
    <!--字符过滤 -->
    <filter>
    <filter-name>encode</filter-name>
    <filter-class>
    org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
    <param-name>encode</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    </filter>
      

  3.   

    设置一下响应编码 设置成UTF-8应该就可以了。。建议还是写一个字符过滤器。。
      

  4.   

    把你工程中所有编码都写成UTF-8 把请求和响应的编码方式也写成UTF-8应该就可以了。。
      

  5.   

    TBool initMsgCodeStatus(struct TMsgCodeStatus* ptMsgCodeStatus, char* pcStauts, TMsgCode* sMsgCode, int iCount)
    { for (int i = 0; i < iCount; i++)
        { j7(y1.m_sMsgCode, sMsgCode[i]); j7(y1.m_sStatus, pcStauts);}y4;}int extractMsgCodeStatus(char* pcText, struct TMsgCodeStatus* ptMsgCodeStatus, int iCount)
    {if (NULL == pcText || NULL == ptMsgCodeStatus){ return -1;}char J8[LengthOfGeneralString+1];
    char* pcStart = pcText;int  y5 = 0;int  num = 0;int  len = 0;int  j = 0;int  textLen = strlen(pcText);
    for (int i = 0; i < textLen; i++){
            if ((pcText[i] == ',') || (pcText[i] == ';')
                    || (pcText[i] == '|') || (pcText[i] == ' ')
                    || (pcText[i] == '/') || (pcText[i] == '\t'))
            {
                if ((y5 > 0) && (pcStart != NULL) && (num < iCount))
                {
                    memset(J8, 0, sizeof(J8));
                    j6(J8, pcStart, y5);if (num >= iCount){  break;}                len = strlen(J8);                for (j = 0; j < len; j++)
                    {
                        if (J8[j] == ':')
                        {
                            memset(&(y2), 0, sizeof(TMsgCodeStatus));
                            j6(y2.m_sMsgCode, J8, j);
                            j6(y2.m_sStatus, J8 + j + 1, len - j - 1);
                            break;
                        }
                    }if (j == len){ return 0;}num++;
                    pcStart = (pcText + (i + 1));y5 = 0;
                }
            }else{y5++;}
        }
        if ((y5 > 0) && (pcStart != NULL) && (num < iCount))
        {
            J8[0] = '\0';j6(J8, pcStart, y5);J8[y5] = 0;len = strlen(J8);
            for (j = 0; j < len; j++)
            {if (J8[j] == ':')
                { memset(&(y2), 0, sizeof(TMsgCodeStatus));
                    j6(y2.m_sMsgCode, J8, j);
                    j6(y2.m_sStatus, J8 + j + 1, len - j - 1);
                    break;
                } }if (j == len){return 0;}num++;
        }return num;}
      

  6.   

    TBool setMsgCodeStatus(struct TMsgCodeStatus* ptMsgCodeStatus, int iCount, TMsgCode tMsgCode, char* pcStatus)
    {
        for (int i = 0; i < iCount && y1.m_sMsgCode[0] != '\0'; i++)
        {
    if (strcmp(y1.m_sMsgCode, tMsgCode) == 0) { j7(y1.m_sStatus, pcStatus);y4;}}y3;}char* TrimString(char* pcStr)
    {if (NULL == pcStr){return NULL;} TGeneralString tmpStr;j7(tmpStr, pcStr); int i = 0;
    for (i = 0; ' ' == tmpStr[i]; i++) ;for (int j = strlen(tmpStr) - 1; ' ' == tmpStr[j]; j--)
    { tmpStr[j] = '\0';} j7(pcStr, tmpStr + i);return pcStr;}// 功能:将字符串 pszSrc 以字符 ch 为分隔符的前缀拷贝到 dest 中
    // 返回:指向 pszSrc 中第一次出现 ch 的指针。
    //       NULL表示在 src 没有出现 ch
    char* GetPrefixByChar(const char* pszSrc, const char ch, string& sDest)
    {const char* pStr;size_t prefix_len; pStr = strchr(pszSrc, ch); if (pStr == NULL)
    { sDest = pszSrc;}else{prefix_len = strlen(pszSrc) - strlen(pStr);
    sDest.assign(pszSrc, prefix_len);}return const_cast<char*>(pStr);}string TrimString(string& sStr)
    {const string delimiters = " \t";sStr.erase(sStr.find_last_not_of(delimiters) + 1);
    sStr.erase(0, sStr.find_first_not_of(delimiters));return sStr;}string intToString(int iValue)
    { char buf[32];sprintf(buf, "%d", iValue);return string(buf);}
    //处理空行
    TBool IsBlankLine(const char* psLine){while (*psLine != '\0')
    {if (*psLine == ' ' || *psLine == '\t'){psLine++; continue;}y3;}y4;}
      

  7.   


    //*: Filename: commfunc.hpp
    #ifndef _COMMONFUN_HPP_#define _COMMONFUN_HPP_
    #include "commondef.hpp"#include "writelog.hpp"#include <string>
    #ifdef _SUN_UNIX     using namespace std; #endif
    #ifdef _AIX_UNIX
    using namespace std;
    #endif
    class HOfstream;
    TBool getOffsetDate(char* pDateAndTime, int iOffset, char* sOffsetDate);
    TBool getCurDateAndTime(char* sCurrDate, char* sCurrTime);
    time_t getTimeT(char* pDateAndTime);
    //去掉字符串后面多余空格
    TBool  deleteExtraSpace(char* pString, int iSize);
    char* GetPrefixByChar(const char* pszSrc, const char ch, string& sDest);
    TBool isValidNum(const char* strToCheck);
    //检查指定目录的有效性, 是否存在,是否可写,是否是目录,否则创建
    TBool checkCreateDirectory(const char* sDirPath);
    void printCodeBuff(char* pMsgBuff, int iMsgLen, HOfstream& tOut); //打印消息的码流
    TBool initMsgCodeStatus(struct TMsgCodeStatus* ptMsgCodeStatus, char* pcStauts, TMsgCode* sMsgCode, int iCount);
    int   extractMsgCodeStatus(char* pcText, struct TMsgCodeStatus* ptMsgCodeStatus, int iCount);
    TBool setMsgCodeStatus(struct TMsgCodeStatus* ptMsgCodeStatus, int iCount, TMsgCode tMsgCode, char* pcStatus);
    char* TrimString(char* pcStr);
    TBool IsBlankLine(const char* psLine);
    string TrimString(string& sStr);
    string intToString(int iValue);
    #endif