java web工程中文乱码问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013673252/article/details/68921987

以前只是看别人的文章,现在想把做过的项目每一步都整理起来,算是做为一个记录吧。

中文乱码问题可能是很多很多方面导致的:

1、jsp中默认是 ISO-8859-1 编码,所以应该改成 UTF-8 如:

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

2、web.xml中可以配置过滤器,现在公司项目基本上都用spring了,可以这样设置

<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


但是此设置只适合post方式,不适用get方式。

3、tomcat中的server.xml中这样设置

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

最后加上 URIEncoding="UTF-8" 就没问题了。

好了,就写这么多吧。



猜你喜欢

转载自blog.csdn.net/u013673252/article/details/68921987