JAVA常见中文问题的解决方法

http://www.java-cn.com/club/article-5876-1.html

以下解决方案是笔者在日常生活中遇到的,希望能对你解决JAVA中文问题有所帮助。

1.在jsp页面首部加上<%@page contentType=”text/html;charset=GB2312“%>
在servlet中使用httpServlerResponse.setContentTpye(“text/html; charset=GB2312”);可以避免一些中文问题

2.使用JDBC连接mysql数据库时,连接字符串写成如下形式可以避免一些中文问题:
jdbc://mysql://hostname:port/DBname?user=username&password=pwd&
useUnicode=true&characterEncoding= iso-8859-1
如果是以数据源的方式连接数据库在配置文件中使用:
<parameter>
<name>url</name>
<value>jdbc:mysql://hostname:port/DBname? &useUnicode=true&characterEncoding=iso-8859-1</value>
</parameter>
注意要使用&替换&符号,否则XML文件在解析的时候会出错。

3.从数据库读出的数据有可能是乱码,遇到这种问题可以用如下方法解决:
String desc = rs.getString(“desc”);
desc = new String(desc.getBytes(“ISO-8859-1”),”GB2312”);

4.某个页面提交中文内容给Servlet,Servlet要对提交的内容进行转码工作才能正确接收数据,
通常我们是在servlet中增加以下代码来解决问题。
httpServlerRequest.setCharacterEncoding(“GB2312”);

5. 在struts中,对资源文件进行转码,使用JDK字带的转码工具:
>native2ascii -encoding BG2312 Myresource.properties Myresource_zh.properties

6.在struts中扩展org.apache.struts.action.RequestProcessor类,并覆写其中的processPreprocess()方法:
package com.mypro.action;

public class MyProRequestProcessor extends RequestProcessor
{
protected boolean processPreprocess (HttpServletRequest request,
HttpServletResponse response)
{
try
{
request.setCharacterEncoding(“GB2312”);
//other code
}
catch(Exception e){}

return true;
}
}
写完上面代码别忘了修改struts-config.xml:
<controller processorClass=” com.mypro.action.MyProRequestProcessor”/>

7. 用filter实现(推荐)
package com.kefeiannan;

import java.io.IOException;
import javax.servlet.*;

public class SetCharacterEncodingFilter implements Filter
{

protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;

public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
配置你站点下的web.xml,在<web-app>后面加上
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.kefeiannan.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

JAVA常见中文问题的解决方法(转)的更多相关文章

  1. coreseek常见错误原因及解决方法

    coreseek常见错误原因及解决方法 Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和 ...

  2. Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  3. php 使用phpmailer 发送邮件(附带中文乱码的解决方法)

    下载phpmailer ,在程序里包含class.phpmailer.php 类  ,这里有中文乱码的解决方法 实例代码如下 <html> <head> <title&g ...

  4. Java并发编程:Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  5. 【转】Java ConcurrentModificationException异常原因和解决方法

    原文网址:http://www.cnblogs.com/dolphin0520/p/3933551.html Java ConcurrentModificationException异常原因和解决方法 ...

  6. ADB几种常见的错误及解决方法

    下面列举出几种常见的错误及解决方法. Q1:无效的安装包,安装包已损坏[INSTALL_FAILED_INVALID_APK] A1:请检查安装包是否完整.如果是xpk包,可以通过 手动安装xpk来检 ...

  7. java.net.SocketException: Connection reset 解决方法

    java.net.SocketException: Connection reset 解决方法 最近纠结致死的一个java报错java.net.SocketException: Connection ...

  8. Django的admin管理系统写入中文出错的解决方法/1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation ‘locate’

    Django的admin管理系统写入中文出错的解决方法 解决错误: 1267  Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and ( ...

  9. JMeter 查看结果树监听器响应数据中文显示乱码解决方法

    查看结果树监听器响应数据中文显示乱码解决方法 by:授客 QQ:1033553122   问题描述 使用监听器-查看结果树(View Results Tree Listener)过程中发现,响应数据面 ...

随机推荐

  1. 题解 P4140 【奇数国 】

    题目链接 首先,按照题意,把前$60$个素数打出来$[2$ $-$ $281]$. 因为只有$60$个,再加上本宝宝极其懒得写线性筛于是每一个都$O(\sqrt{n})$暴力筛就好了. 代码如下: # ...

  2. Pycharm使用的一些问题!!!

    1.pycharm如何更改主题.如何更改字体的大小? 2.pycharm如何import第三方库? 1.更改主题和字体大小 主题变暗,字体变大! 2.如何导入第三方库?

  3. C++ #include " " 与 <>有什么区别?

    #include <> 和 #include "" 都会在实现定义的位置查找文件,并将其包含. 区别是若 #include "" 查找成功,则遮蔽 ...

  4. 批量改ID 行形式

    update [Temp_Sql] set ID(字段) = (274+ID(字段))  字段+字段形式 可以以行形式批量修改

  5. springboot访问静态资源遇到的坑

    开始是以这种结构进行的,结果页面上一篇红,访问的页面是这样的 最终找出来问题,虽然每次调整路径都不对,最终查看多种方法可以看到了: 增加: package com.example.demo.confi ...

  6. 洛谷 P3239 / loj 2112 [HNOI2015] 亚瑟王 题解【期望】【DP】

    ???看不懂的期望DP 题目描述 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知,亚 ...

  7. UVA - 11388 唯一分解定理

    题意:给出G和L,求最小的a使得gcd(a,b)=G,lcm(a,b)=L 显然a>=G,所以a取G,b要满足质因子质数为L的同次数,b取L //此处应有代码

  8. [转]【NODE】用WS模块创建加密的WS服务(WSS)

    [From] https://luojia.me/2015/07/21/%E3%80%90node%E3%80%91%E7%94%A8ws%E6%A8%A1%E5%9D%97%E5%88%9B%E5% ...

  9. 【研究】Struts2-048漏洞

    1.1 漏洞背景 2017年7月7日,Apache Struts发布最新的安全公告,Apache Struts2-strus1-plugin插件存在远程代码执行的高危漏洞,漏洞编号为CVE-2017- ...

  10. 关于i18n

    现在工作主要负责小程序端,很少负责backend.最近的一个任务是配置多语言.因为一开始都是写死的中文,现在需要把那些变成英文. 狂搜了一波,其实网上的方法都不怎好.(可能就是一开始看的时候觉得好.) ...