【问题标题】:Htaccess changes the content type of page?Htaccess 改变页面的内容类型?
【发布时间】:2011-04-18 08:19:20
【问题描述】:

我的 Java 端有该代码:

....
PrintWriter writer = response.getWriter();
response.setContentType("application/json; charset=utf-8");
response.setCharacterEncoding("UTF-8");
....

我在 MySQL 和 Apache 上的应用程序中使用 Struts2、JSP 和 Java。当我写一些东西到响应对象并检查萤火虫的内容类型时,它总是

charset=ISO-8859-1

所以,当我想在响应对象上写一些东西时,会出现无法识别的字符。

我的 jsp 文件每次都有那一行:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

我测试了我的程序,当它在不通过 .htaccess 的情况下工作时一切正常。我知道 .htaccess 改变了内容类型。

我在这里尝试过类似的东西:

http://www.askapache.com/htaccess/setting-charset-in-htaccess.html

但我不确定我是否错过了什么或尝试其他什么?

【问题讨论】:

    标签: java .htaccess utf-8 response content-type


    【解决方案1】:

    尝试在您的 JSP 页面标题中指定编码,如下所示:

    <%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" trimDirectiveWhitespaces="true" %>
    

    【讨论】:

      【解决方案2】:

      既然您使用response.setCharacterEncoding() 设置字符编码,为什么不直接设置Content-Type 而不指定字符集?

      response.setContentType("application/json");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-16
        • 2010-12-05
        • 2011-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-18
        相关资源
        最近更新 更多