【问题标题】:Set response encoding in embedded tomcat在嵌入式 tomcat 中设置响应编码
【发布时间】:2016-02-15 11:32:48
【问题描述】:

有没有办法在嵌入式 tomcat 中设置响应编码?到目前为止,我只能找到设置 URI 编码。但我需要设置响应编码。

令人惊讶的是,独立的 tomcat 默认以 UTF-8 格式发送数据。

【问题讨论】:

  • 如果不想使用过滤器,只需使用 HttpServletResponse 中可用的 ServletResponse 类的 setCharacterEncoding 方法来设置响应编码。

标签: java apache tomcat catalina


【解决方案1】:

您可以创建一个过滤器来设置您的响应编码

import java.io.IOException;  
import java.io.PrintWriter;  

import javax.servlet.*;  

public class MyFilter implements Filter{  

public void init(FilterConfig arg0) throws ServletException {}  

public void doFilter(ServletRequest req, ServletResponse resp,  
    FilterChain chain) throws IOException, ServletException {  

    resp.setCharacterEncoding("text/plain; charset=UTF-8"); 

    chain.doFilter(req, resp);//sends request to next resource  


    }  
public void destroy() {}  
}  

【讨论】:

  • 我正在使用嵌入式 tomcat。我尝试设置 setCharacterEncoding("UTF-8")。尽管 HTTP 响应标头发送了编码,但浏览器没有正确显示字符。
  • 使用 setCharacterEncoding("text/html; charset=UTF-8");见link
  • 但是,问题是它在码头和独立的 tomcat 上运行良好。嵌入式tomcat版本是8.0.12,是这个原因吗?
  • Servlet 规范对于所有容器都是相同的。问题是另一回事。请尝试在 web.xml 中添加这个 *.jspUTF-8
  • 我没有使用 JSP。它直接提供 html。
【解决方案2】:
  1. 创建过滤器:

    `package charsetFilter.classes;

     import java.io.IOException;
     import javax.servlet.Filter;
     import javax.servlet.FilterChain;
     import javax.servlet.FilterConfig;
     import javax.servlet.ServletException;
     import javax.servlet.ServletRequest;
     import javax.servlet.ServletResponse;
    
    public class CharsetFilter implements Filter{
        private String encoding;
    
        public void init(FilterConfig config) throws ServletException{
                encoding = config.getInitParameter("requestEncoding");
                if( encoding==null ) encoding="UTF-8";
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain       next)
        throws IOException, ServletException{
            // Respect the client-specified character encoding
            // (see HTTP specification section 3.4.1)
                if(null == request.getCharacterEncoding())
                request.setCharacterEncoding(encoding);
                /**
            * Set the default response content type and encoding
            */
            response.setContentType("text/html; charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
                next.doFilter(request, response);
        }
    
            public void destroy(){}
    }`
    
  2. 在 web.xml 中将此过滤器添加为:

    `<filter>
            <filter-name>CharsetFilter</filter-name>
            <filter-class>charsetFilter.classes.CharsetFilter</filter-class>
                <init-param>
                    <param-name>requestEncoding</param-name>
                    <param-value>UTF-8</param-value>
                </init-param>
    </filter>
    
    <filter-mapping>
            <filter-name>CharsetFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>`
    
  3. HTML-meta 标签应该写在所有 HTML 文件中:
    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi"> <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />

  4. 将您的 servlet 设置为:
    request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8");

  5. 处理 servlet 请求为:
    String input = new String(request.getParameter("foo").getBytes("iso-8859-1"), "utf-8"); String input = URLDecoder.decode(request.getParameter("keyWord"), "UTF-8");

如果您还有问题,请告诉我

【讨论】:

    猜你喜欢
    • 2019-01-13
    • 1970-01-01
    • 2018-02-22
    • 2014-10-20
    • 2011-10-26
    • 2012-08-20
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多