Tomcat UTF-8问题

时间:2013-07-15 13:59:12

标签: java windows tomcat utf-8

我有一个非常奇怪的问题,我在同一个Tomcat 6实例上运行了两个Web应用程序。但是,一个Web应用程序正确发布了UTF-8字符而另一个Web应用程序没有,我无法弄清楚原因。

server.xml文件包含以下行:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8443" />

每个WEB-INF / web.xml都有以下几行:

<filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>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>

<context-param>
    <param-name>PARAMETER_ENCODING</param-name>
    <param-value>UTF-8</param-value>
</context-param>

每个Web应用程序都有相同的java类EncodingFilter,位于WEB-INF \ classes文件夹中,java类如下:

import java.io.IOException;
import javax.servlet.*;

public class EncodingFilter
    implements Filter
{

    public EncodingFilter()
    {
    }

    public void init(FilterConfig filterconfig)
        throws ServletException
    {
        filterConfig = filterconfig;
        encoding = filterConfig.getInitParameter("encoding");
    }

    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
        throws IOException, ServletException
    {
        servletrequest.setCharacterEncoding(encoding);
        filterchain.doFilter(servletrequest, servletresponse);
    }

    public void destroy()
    {
    }

    private String encoding;
    private FilterConfig filterConfig;
}

我已检查过JAVA环境设置:

file.encoding=UTF-8
sun.jnu.encoding=UTF-8
sun.io.unicode.encoding=UnicodeLittle
file.encoding.pkg=sun.io

但是下面的JSP文件在一个Web应用程序中正确发布而不是另一个Web应用程序中的踢球者:

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Character encoding test page</title>
</head>
<body>
<p>Data posted to this form was:
<%
request.setCharacterEncoding("UTF-8");
out.print(request.getParameter("mydata"));
%>
</p>
<form method="POST" action="test.jsp">
<input type="text" name="mydata">
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
</body>
</html>

如果我发布'çanakçömlekpatladı',我会得到'çanakçömlekpatladı',如果我在其他网络应用程序中发布我得到'çanakçömlekpatladı'我真的不明白为什么,他们都使用相同的文件,在同一个Tomcat实例上,所以为什么我得到的结果不同,我可能隐藏的是什么?

非常感谢任何帮助: - )

1 个答案:

答案 0 :(得分:2)

我发现了这个问题,还有另一个java类MaintenanceModeFilter,它有一个影响因素:

package com.jenkov.seo.web;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

public class MaintenanceModeFilter
    implements Filter
{

    public MaintenanceModeFilter()
    {
        mode = 0;
        maintenanceUrl = null;
        maintenanceGuiUrl = null;
        password = null;
        comment = null;
    }

    public void init(FilterConfig filterconfig)
        throws ServletException
    {
        maintenanceUrl = filterconfig.getInitParameter("maintenanceUrl");
        maintenanceGuiUrl = filterconfig.getInitParameter("maintenanceGuiUrl");
        password = filterconfig.getInitParameter("password");
    }

    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
        throws IOException, ServletException
    {
        HttpServletRequest httpservletrequest = (HttpServletRequest)servletrequest;
        if(servletrequest.getParameter("maintenance-mode") != null)
        {
            if(password.equals(servletrequest.getParameter("password")))
            {
                mode = Integer.parseInt(servletrequest.getParameter("maintenance-mode"));
                comment = servletrequest.getParameter("aComment");
                servletrequest.getRequestDispatcher(maintenanceGuiUrl).include(servletrequest, servletresponse);
            }
            return;
        }
        if(mode != 0 && !httpservletrequest.getServletPath().equals(maintenanceGuiUrl))
        {
            servletrequest.setAttribute("comment", comment);
            servletrequest.getRequestDispatcher(maintenanceUrl).include(servletrequest, servletresponse);
            return;
        } else
        {
            filterchain.doFilter(servletrequest, servletresponse);
            return;
        }
    }

    public void destroy()
    {
    }

    public static final int MODE_NORMAL_OPERATION = 0;
    protected int mode;
    protected String maintenanceUrl;
    protected String maintenanceGuiUrl;
    protected String password;
    protected String comment;
}

我已从web.xml文件中删除了引用并重新启动了tomcat服务器,并且这两个Web应用程序都支持UTF-8。看来,在调用HttpServletRequest时,MaintenanceModeFilter类重写了编码。

我希望这有助于其他人,上述设置在设置Tomcat服务器和支持UTF-8的Web应用程序时非常有用。