web网页汉语变成乱码
web网页中文变成乱码
碰到一个问题,在web页面上面中文变成乱码,也不是所有中文,只是在jscript文件中的中文都变成了乱码:
当时解决步骤比较凌乱,也不知道哪个是真正生效,不过问题是解决了。
记录如下
1.
tomcat:server.xml
<Connector port="10005" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="18443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
2.
.bashrc
LANG=zh_CN.UTF-8
LC_CTYPE=zh_CN.UTF-8
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE=zh_CN.UTF-8
LC_MONETARY=en_US.UTF-8
LC_MESSAGES=zh_CN.UTF-8
LC_PAPER=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_ALL=
3.
web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<init-param>
<param-name>fileEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
4.
web.xml
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.deepblue.backend.management.filter.CharsetFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
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() {
}
}
5. 记得别忘记强制刷新网页
碰到一个问题,在web页面上面中文变成乱码,也不是所有中文,只是在jscript文件中的中文都变成了乱码:
当时解决步骤比较凌乱,也不知道哪个是真正生效,不过问题是解决了。
记录如下
1.
tomcat:server.xml
<Connector port="10005" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="18443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
2.
.bashrc
LANG=zh_CN.UTF-8
LC_CTYPE=zh_CN.UTF-8
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE=zh_CN.UTF-8
LC_MONETARY=en_US.UTF-8
LC_MESSAGES=zh_CN.UTF-8
LC_PAPER=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_ALL=
3.
web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<init-param>
<param-name>fileEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
4.
web.xml
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.deepblue.backend.management.filter.CharsetFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
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() {
}
}
5. 记得别忘记强制刷新网页