欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

项目中常用处理乱码的手段

程序员文章站 2022-03-06 09:48:32
...

 

1、jsp页面指定字符集

   这个主要是处理页面显示的乱码问题

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert title here</title>
</head>
<body>

</body>
</html>

 

 

2、修改tomcat配置文件,tomcat根目录/conf/server.xml

 

  修改Connector,添加URIEncoding="UTF-8" 属性,这个主要是处理get请求返回的数据。

 

  

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

 

 

3、web应用中添加filter过滤器

 

    编写过滤器

 

     EncodingFilter.java

 

 

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 EncodingFilter implements Filter {
	protected String encoding = null;

	protected FilterConfig filterConfig = null;

	public void destroy() {
		this.encoding = null;
		this.filterConfig = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		String encoding = selectEncoding(request);
		if (encoding != null) {
			request.setCharacterEncoding(encoding);
			response.setCharacterEncoding(encoding);
		}

		chain.doFilter(request, response);
	}

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

	protected String selectEncoding(ServletRequest request) {
		return this.encoding;
	}
}

 

    web.xml中配置过滤器

 

    

<filter>
    <filter-name>SetChartEncoding</filter-name>
    <filter-class>com.hotels.common.EncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>SetChartEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 

4、response中返回header设置content-type=“utf-8”

 

    

// 在一个servlet 中 或者在spring的一个controller中
protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path = request.getContextPath();/// hotels
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

}

 

 

5、如果是spring项目,可以添加spring的字符集过滤器

 

   web.xml

   

<!-- 字符集 -->
	<filter>
		<filter-name>characterEncoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

 

    

 

 

 

 

相关标签: jsp encoding