2

I have a very strange issue, I have two web apps running on the same Tomcat 6 instance. However one web app posts UTF-8 characters correctly and the other web app doesn't and I can't figure out why.

The server.xml file has the line:

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

Each WEB-INF/web.xml has the following lines:

<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>

Each web app has the same java class EncodingFilter located in the WEB-INF\classes folder, the java class is as follows:

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;
}

I have checked the JAVA environment settings:

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

But here is the kicker the following JSP file posts correctly in one web app but not the other:

<%@ 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>

If I post 'çanak çömlek patladı' in one I get 'çanak çömlek patladı', if I post in the other web app I get 'çanak çömlek patladı' I really can't understand why, they both use the same files, on the same Tomcat instance, so why on earth am I getting a different result, what could be hidden that I am missing?

Any help would be much appreciated :-)

4

1 回答 1

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 服务器和 Web 应用程序以支持 UTF-8 时应该很有用。

于 2013-07-16T10:23:07.357 回答