关于tomcat乱码问题的解决
(一) 更改 C:\Tomcat\conf\server.xml,指定浏览器的编码格式为“简体中文”:
方法是找到 server.xml 中的
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='GBK' />
这时地址栏中就可传递中文了,对于tomcat5.0 不加 URIEncoding='GBK' 是不能传递中文的,有中文传递时只能变成乱码。
(二)如servlet不能正常显示汉字 则加入
public class ThreeParams extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");
...
}
}
作用是把页面的内容定义为中文字集gb2312.
(三)如果从数据库中抽取的数据是乱码需要加过滤器
实现如下:
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;
import javax.servlet.UnavailableException;
public class SetCharacterEncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)throws IOException, ServletException {
request.setCharacterEncoding("gb2312");
// 传递控制到下一个过滤器
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
配置web.xml
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(四)字符的转换,如在读取文件内容,数据库内容出现乱码时,可由以下办法解决。
public class toGB2312
{
public toGB2312()
{
//
}
public String Gb2312(String uniC){
String gb2312Str = "";
if(uniC == null){
uniC = "";
}
try{
byte[] tep = uniC.getBytes("ISO8859_1");
gb2312Str = new String(tep,"GB2312");
}
catch(Exception ex){
}
return gb2312Str;
}
public String UniC(String gb2312Str){
String unicoStr = "";
if(gb2312Str == null){
gb2312Str = "";
}
try{
byte[] yte = gb2312Str.getBytes("GB2312");
unicoStr = new String(yte,"ISO8859_1");
}catch(Exception ex){
}
return unicoStr;
}
}
你也可以在直接的转换,首先你将获取的字符串用ISO-8859-1进行编码,然后将这个编码存放到一个字节数组中,然后将这个数组转化成字符串对象就可以了,例如:
String str=request.getParameter(“girl”);
Byte B[]=str.getBytes(“ISO-8859-1”);
Str=new String(B);
通过上述转换的话,提交的任何信息都能正确的显示。
关于tomcat乱码问题的解决
(一) 更改 C:\Tomcat\conf\server.xml,指定浏览器的编码格式为“简体中文”:
方法是找到 server.xml 中的
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='GBK' />
这时地址栏中就可传递中文了,对于tomcat5.0 不加 URIEncoding='GBK' 是不能传递中文的,有中文传递时只能变成乱码。
(二)如servlet不能正常显示汉字 则加入
public class ThreeParams extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");
...
}
}
作用是把页面的内容定义为中文字集gb2312.
(三)如果从数据库中抽取的数据是乱码需要加过滤器
实现如下:
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;
import javax.servlet.UnavailableException;
public class SetCharacterEncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)throws IOException, ServletException {
request.setCharacterEncoding("gb2312");
// 传递控制到下一个过滤器
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
配置web.xml
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(四)字符的转换,如在读取文件内容,数据库内容出现乱码时,可由以下办法解决。
public class toGB2312
{
public toGB2312()
{
//
}
public String Gb2312(String uniC){
String gb2312Str = "";
if(uniC == null){
uniC = "";
}
try{
byte[] tep = uniC.getBytes("ISO8859_1");
gb2312Str = new String(tep,"GB2312");
}
catch(Exception ex){
}
return gb2312Str;
}
public String UniC(String gb2312Str){
String unicoStr = "";
if(gb2312Str == null){
gb2312Str = "";
}
try{
byte[] yte = gb2312Str.getBytes("GB2312");
unicoStr = new String(yte,"ISO8859_1");
}catch(Exception ex){
}
return unicoStr;
}
}
你也可以在直接的转换,首先你将获取的字符串用ISO-8859-1进行编码,然后将这个编码存放到一个字节数组中,然后将这个数组转化成字符串对象就可以了,例如:
String str=request.getParameter(“girl”);
Byte B[]=str.getBytes(“ISO-8859-1”);
Str=new String(B);
通过上述转换的话,提交的任何信息都能正确的显示。