tomcat设置编码utf8

1.       Java类: CharacterEncodingFilter

 import javax.servlet.*;

import java.io.IOException;

public class CharacterEncodingFilter implements Filter {

private String encoding = "UTF-8";

private boolean forceEncoding = true;

/**

* Set the encoding to use for requests. This encoding will be

* passed into a ServletRequest.setCharacterEncoding call.

* <p>Whether this encoding will override existing request

* encodings depends on the "forceEncoding" flag.

*/

public void setEncoding(String encoding) {

this.encoding = encoding;

}

/**

* Set whether the encoding of this filter should override existing

* request encodings. Default is "false", i.e. do not modify encoding

* if ServletRequest.getCharacterEncoding returns a non-null value.

*/

public void setForceEncoding(boolean forceEncoding) {

this.forceEncoding = forceEncoding;

}

public void init(FilterConfig filterConfig) throws ServletException {

this.encoding = filterConfig.getInitParameter("encoding");

String force = filterConfig.getInitParameter("forceEncoding");

this.forceEncoding = (force == null) || Boolean.valueOf(force).booleanValue() ;

}

public void doFilter(ServletRequest servletRequest,

ServletResponse servletResponse,

FilterChain filterChain) throws IOException, ServletException {

if (this.forceEncoding || servletRequest.getCharacterEncoding() == null) {

servletRequest.setCharacterEncoding(this.encoding);

}

filterChain.doFilter(servletRequest, servletResponse);

}

public void destroy() {

}

}

2.       在web.xml配置文件里加Filters Configurations

 <!-- Filters Configurations        -->

<filter>

<filter-name>CHARACTER_ENCODING</filter-name>

<filter-class>

common.encoding.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>CHARACTER_ENCODING</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

3.数据库建立为UTF-8格式 
4.项目右键属性为UTF-8格式 
5.所有页面申明为UTF-8 
6.JDBC URL设为:UTF-8 

jdbc:MySQL://localhost:3306/databaseuse?useUnicode=true&characterEncoding=utf-8 
7.数据库Driver选择UTF-8格式 
8.Tomcat编码改成UTF-8 

在server.xml里面增加URIEncoding="UTF-8"

<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />

时间: 2024-10-17 23:11:21

tomcat设置编码utf8的相关文章

cookie中maxAge总是-1和tomcat设置编码方式

指定tomcat的URI编码方法 tomcat中conf文件夹下修改server.xml文件,在connector属性中添加语句 URIEncoding="UTF-8" useBodyEncodingForURI="true" 利用cookie存储了登录名loinname字段并设置maxAge为2天,结果再次访问localhost:8080/a/1.do时.在相对于的servlet的request中,reques.getCookie()可以拿到相应的cookie,但

设置Tomcat编码(UTF-8)

Tomcat的默认编码是ISO-8859-1,如果有是get请求时,会出现乱码,这种情况可以修改Tomcat的编码解决. 在tomcat的conf目录下,编辑server.xml配置文件,在Connector节点,加上URIEncoding="UTF-8"

TomCat控制台中文乱码及IDEA设置为UTF-8

一.解决IDEA中的中文乱码 1.首先设置idea编辑器的编码: File-Setting设置如下 idea显示编码:windows默认用gbk所以idea显示默认为gbk编码,在 Help--Edit custom vm options 添加-Dfile.encoding=UTF-8 (其实大部分问题就是没设置好这个) 2.然后配置tomcat的编码问题: 打开idea安装目录-bin 用记事本打开idea64.exe.vmoptions文件 在文件后面添加一行:-Dfile.encoding

linux 设置mysql 数据库编码utf8

转载地址 http://www.linuxidc.com/Linux/2015-08/121676.htm 在MySQL数据库中, 当插入数据无法支持中文时, 可将该数据库的编码集设置为utf8, 故在创建数据库时, 将数据库编码集及其他字符编码设置为utf8. 如果之前创建的数据库的数据库编码集不为utf8, 可以进行如下设置: mysql> alter database 数据库名 character set utf8; 或删除原有旧的数据库, 1117.www.qixoo.qixoo.com

python基础-PyCharm设置作者信息模板_修改解释器_设置软件UTF-8编码

一.PyCharm 设置作者信息模板 1.File---Settings---在搜索框中搜索:File and Code Templates---Python scripts 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: huzhihua 二.修改解释器方法 File---settings---project:python---project interpreter 三.设置软件UTF-8 编码File---Setting

mysql设置编码为UTF8,设置表为UTF8。

1.设置编码为UTF-8 set character_set_client = utf8; set character_set_server = utf8; set character_set_connection = utf8; set character_set_database = utf8; set character_set_results = utf8; set collation_connection = utf8_general_ci; set collation_databas

mysql 5.5 编码设置为utf8 转载自:http://outofcontrol.ca/thoughts/comments/change-mysql-5.5-default-character-set-to-utf8

Change MySQL 5.5 default character-set to UTF8 连接里是linux下的 在window下my.ini Add under [client]  the following code snippet: loose-default-character-set = utf8 Add under [mysqld] the following code snippet:init_connect=‘SET collation_connection = utf8_u

windows设置新建文本文档默认编码UTF-8

1. 打开记事本新建一个空白的文本文档,不输入任何文字,然后保存此文档,在"另存为"对话框中将编码由默认的 ANSI 修改为 Unicode 或 UTF-8,接着为文件取名,在此假设将新文档命名为 UNICODE.TXT. 2. 将 utf-8.txt 复制至隐含的系统文件夹 C:\Windows\ShellNew. 3. 打开注册表编辑器定位至:HKEY_CLASSES_ROOT\.txt\ShellNew,新建名为 FileName 的字符串值,将此字符串值设置为 utf-8.tx

IDEA正确设置编码统一为UTF-8

之前代码在myeclispe10跑得好好的来这个intellij idea 就一直出错 改了好久的编码都没卵用,如下设置才正确.还有idea的web工程目录和myeclispe的目录是不一样的,神坑. File -> Settings->File Encodings -> Defaule encoding for properties files 属性设置成utf-8 选中Transparent native-to-ascii conversion 作者:Java码海无涯 来源:CSDN