Java 如何将页面编码包含到所有jsp页面中

Java 如何将页面编码包含到所有jsp页面中,java,jsp,utf-8,web.xml,Java,Jsp,Utf 8,Web.xml,我在一个jsp页面中包含了,它正确地显示了法语字符。 但是我想将页面编码包含到所有jsp页面中。 在web.xml中,我包括了 <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-gr

我在一个jsp页面中包含了
,它正确地显示了法语字符。 但是我想将页面编码包含到所有jsp页面中。 在web.xml中,我包括了

<jsp-config>
      <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
      </jsp-property-group>
 </jsp-config>

*.jsp
UTF-8
但它将适用于tomcat7,我使用的是tomcat6


是否有其他解决方案?

如果在使用Tomcat时您的参数不是UTF-8编码的,请尝试调整Tomcat的server.xml中的连接器配置,如下所示:

您可以在web.xml中定义一个新的过滤器,并在那里修改您的答案

例如,您可以将其放在web.xml中:

<filter>
    <filter-name>encoding</filter-name>
    <filter-class>com.example.EncodingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*.jsp</url-pattern> 
    <!-- or any pattern you like to excluse non-jsp resources -->
</filter-mapping>
您还可以在
setCharacterEncoding()
调用周围添加测试,以仅过滤特定URI或排除非HTML jsp或特定参数,例如,如果提供了URL参数
format=PDF
,则jsp输出PDF:

if (!request.getQueryString().contains("format=PDF") {
        response.setCharacterEncoding("utf-8");
}
chain.doFilter(request, response); // pass request to next filter

您可以使用元标记,而不是在所有jsp文件或web.xml中添加页面编码。如果您有一个公共的.jsp文件,请包括


另一种方法是使用过滤器对所有页面应用编码

public void doFilter(ServletRequest request,ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
            request.setCharacterEncoding(encoding);
    //                response.setContentType("text/html;charset=UTF-8");
                    response.setCharacterEncoding(encoding);
            filterChain.doFilter(request, response);

        }

        public void init(FilterConfig filterConfig) throws ServletException {
            String encodingParam = filterConfig.getInitParameter("encoding");
            if (encodingParam != null) {
                encoding = encodingParam;
            }
        }

在web.xml中注册此筛选器

您的后端值有问题吗?JSP中的静态html内容?参数?您的答案是正确的,但如果您能详细说明一下,它会更好。@MaVRoSCy现在应该更好谢谢您的回复,但我只使用jsp和servlet,我没有使用任何框架…我不能使用过滤器…好吧,这只是来自javax.servlet API。。。只要你有一个web.xml,就意味着你可以使用它;我在运行tomcat6i的纯jsp/servlet应用程序上使用这种过滤器,我得到“类型编码过滤器必须实现继承的抽象方法filter.init(FilterConfig)”即使在包含javax.servlet-api之后,这也不能满足用户的要求。你能给我详细的解释吗?你可以选择任何合适的字符集。谢谢你jessica,但我在法语字符显示方面还有一个问题。我有一个文件,我应该在其中保存法语字符,我正在使用Fileinputstream并设置字符集,但是法语字符显示为“?”你能帮我一下吗…你在使用哪个字符集?使用正确的字符集它会工作的。
public void doFilter(ServletRequest request,ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
            request.setCharacterEncoding(encoding);
    //                response.setContentType("text/html;charset=UTF-8");
                    response.setCharacterEncoding(encoding);
            filterChain.doFilter(request, response);

        }

        public void init(FilterConfig filterConfig) throws ServletException {
            String encodingParam = filterConfig.getInitParameter("encoding");
            if (encodingParam != null) {
                encoding = encodingParam;
            }
        }