千家信息网

JAVA常见中文问题的解决方案是怎样的呢

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,JAVA常见中文问题的解决方案是怎样的呢,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。JAVA常见中文问题的解决方法以下
千家信息网最后更新 2026年11月01日JAVA常见中文问题的解决方案是怎样的呢

JAVA常见中文问题的解决方案是怎样的呢,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

以下解决方案是笔者在日常生活中遇到的,希望能对你解决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

如果是以数据源的方式连接数据库在配置文件中使用:

url

jdbc:mysql://hostname:port/DBname? &useUnicode=true&characterEncoding=iso-8859-1

注意要使用&替换&符号,否则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:

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,在后面加上

Set Character Encoding

com.kefeiannan.SetCharacterEncodingFilter

encoding

UTF-8

Set Character Encoding

/*

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

问题 中文 数据 内容 数据库 文件 方法 帮助 常见 方案 解决方案 代码 页面 配置 清楚 乱码 字符 字符串 对此 工具 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 花魁咖啡 互联网科技企业 广州紫鲸互联网科技有限公司 互联网科技财经 广州互联网科技公司 中国 科技 互联网 互联网高科技企业 互联网金融监管科技 衡水科技产业互联网平台 上海电发互联网科技有限公司 青岛中康互联网医疗科技有限公司 互联网科技项目计划书材料类 湖南一路畅行互联网科技 互联网科技和法律行业 山东星卡互联网科技有限公司官网 山东大美朱楼互联网科技有限公司 现代互联网科技公司 互联网科技大会大佬 安徽九吞吴互联网科技有限公司 互联网科技创业有什么好项目 互联网科技工作委员会毛军 科技互联网产品 科技创新与互联网经济 厦门中拓互联网络科技有限公司 广州宏创互联网科技 上海致骋互联网科技 互联网科技产业特征 山东星卡互联网科技有限公司官网 富国互联网金融科技混合基金 西安有哪些互联网高科技企业
0