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

/*

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

网站标题:JAVA常见中文问题的解决方案是怎样的呢
URL分享:https://www.cdcxhl.com/article24/igcjje.html

成都网站建设公司_创新互联,为您提供建站公司自适应网站手机网站建设网站收录网站策划动态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司