2008年9月11日星期四

Web开发中文乱码问题及解决方法

问题描述:用单数中文汉字作为查询条件,在分页的时候,点击下一页,或第几页时 出现问题

在查询条件框里输入 魔戒 能够查询所有正确的数据 点击分页 也可以正常显示数据

但是在查询条件里输入 魔 或者 魔戒三 时,点击查询按钮,能够得到正确的数据,当你点击下一页时,查询条件框显示 魔戒??

假如是单个魔字 哪就显示 ??

假如输入魔戒3或者是魔戒a 不会出现以上问题 所有功能都能实现,没有出现

问题是 不能输入单数的汉字查询 是在它点击下一页时出现问题

struts开发工具

可能是我的描述不够清楚,假如有人愿意帮我解决这个问题,请加我的QQ:376987715
很急!!!!!!!!
谢谢!


网友回复:假如你使用的JSP,你要确定一下你页面的contentType设置是否是GBK或者GB2312,假如使用的是Tomcat ,你要把配置文件中的一向加上编码是GBK,就可以搞定
网友回复:编码转换问题.
网友回复:Tomcat MySql Struts的中文乱码问题


在做以下工作之前,所有的HTML/JSP的charset都设为charset=gb2312。

第一个要解决的是表单提交乱码问题。在使用Struts提供的ActionForm过程中,无论表单采用的是Struts标签还是Html标签,都可以用 ActionForm的Get/Set来获取和设置表单的元素值(它们的作用效果与request.getParameter()方法一样),但提取出来 的数据不经过处理的话就是乱码,主要的原因是1.Tomcat的J2EE实现对表单提交即Post方法提交时,处理参数采用默认的ISO8859_1来处 理2.Tomcat对Get方法提交的请求在query-string处理时采用了和Post方法不一样的处理方式。所以假如要正确地显示和获取中文数据 采用的解决方案:(1)对于Post方法提交的表单通过编写一个过滤器(filer)的方法解决,过滤器在用户提交的数据被处理之前被调用,可以通过这个Java代码改变参数的编码方式(目标编码方式可以通过Web.xml文件里面的参数指定)。过滤器的代码如下:

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;

/**
*

Example filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:


*

    *
  • encoding - The character encoding to be configured
    * for this request, either conditionally or unconditionally based on
    * the ignore initialization parameter. This parameter
    * is required, so there is no default.

  • *
  • ignore - If set to "true", any character encoding
    * specified by the client is ignored, and the value returned by the
    * selectEncoding() method is set. If set to "false,
    * selectEncoding() is called only if the
    * client has not already specified an encoding. By default, this
    * parameter is set to "true".

  • *

*
*

Although this filter can be used unchanged, it is also easy to
* subclass it and make the selectEncoding() method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the Accept-Language
* and User-Agent headers, or a value stashed in the current
* user's session.


*
* @author Craig McClanahan
* @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:33 $
*/

public class SetCharacterEncodingFilter implements Filter {


// ----------------------------------------------------- Instance Variables


/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;


/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;


/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;


// --------------------------------------------------------- Public Methods


/**
* Take this filter out of service.
*/
public void destroy() {

this.encoding = null;
this.filterConfig = null;

}


/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

// Conditionally select and set the character encoding to be used
if (ignore ¦ ¦ (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}

// Pass control on to the next filter
chain.doFilter(request, response);

}


/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
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 Methods


/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* null .
*


* The default implementation unconditionally returns the value configured
* by the encoding initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}

编绎后把class文件放在classes目录下,并在Web应用的web.xml文件中添加如下代码:


Set Character Encoding
com.neusoft.equipment.controller.SetCharacterEncodingFilter

encoding
gbk



Set Character Encoding
/*
只要是gb2312,gbk,utf8等支持多字节编码的字符集都可以储存汉字,当然,gb2312中的 汉字数量远少于gbk,而gb2312,gbk等都可在utf8下编码,这里指定目标编码方式是gbk,重新启动Tomcat后就可以了。
(2)对Get方法提交的表单,由于参数是紧跟在用户的URL请求后面,Tomcat对其的处理方法与Post方法不一样。所以上面设置的过滤器对Get 方法没有作用,它需要在其他地方设置。找到Tomca的server.xml配置文件,找到对80(或者是8080等别的,这个是自己修改后的)的 Connector组件的设置部分,给这个Connector组件添加一个属性:URIEncoding="GBK"。修改后的Connector组件是 这样的:


maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK"/>这样修改后,重启Tomcat就可以正确处理GET方法提交的表单数据了。

第二个要解决的是数据库存取数据出现的乱码等情况。对于不同的数据库往往支持不同的编码,造成了应用时比较混乱,不同的数据库的解决方法往往是不同的,针 对MySql,网上也有各种各样的解决方案,但个人觉得那些太繁了,现在有一个极其简单的解决办法:修改MySql的配置文件,打开MySql安装后的根 目录,找到my.init文件,把[mysqld]区的如下语句:default-character-set=latin1修改为:default- character-set=gbk,然后在[client]区增加:default-character-set=gbk,修改后记得做一件事情,到 Widows控制面板的治理工具下的服务程序,把Mysql服务停止了重新启动,这样就根本解决了MySql的数据库乱码问题,很简单~~~~

网友回复:进行转码啊 很简单的!~
网友回复:同意3楼的,其实那个类在tomcat自带的例子中有
网友回复:1 我确认你的系统采用的UTF-8编码,假如用GBK不会出现单数汉字的乱码问题
2 解决方法,换POST提交吧,这个又简单,又好用
3 看看 google 的搜索吧,他就是 UTF-8的

网友回复:解决乱码问题的方法有两种:
(1)创建函数来解决,通过gb2312来编码解决乱码问题,代码如下:
<%@page contentType="test/html;charset=gb2312"%>
<%@page import="java.io.*"%>
<%!
String change(String str)
{
String trans=null;
Byte temp[];
try
{
temp=str.getBytes("gb2312");
trans=new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println(e.toString());
}

return trans;

}
%>


(2)第一种方法相对而言比较耗费时间,另外还有一种更加简单的方法:

<%@page contentType="test/html;charset=gb2312"%>
<%
request.setCharacterEncoding("gb2312")
%>
网友回复:做个filter
网友回复:常见问题
网友回复:该回复于2008-05-08 15:47:12被版主删除
网友回复:我是写一个java类做分页的功能 传一个url 给java类, 用 发送的,用UTF-8的编码 请问如何解决
网友回复:部分代码


<%=RtPage.getPagination(request,"../program.do?method=queryProgramByTitle&title=" request.getAttribute("title") "&status=" request.getAttribute("status") "&",
"pageIndex", "rowCount", "pageSize")%>



红色表示传入java类里的url
java部分代码:
previousPage = "
" " 3 " " ";

jsp 用UTF-8的编码.用双数汉字可以点击 上下页,用单数汉字就不行,假如是三个汉字,点击下页时,查询条件框,显示前二个汉字是好的,第三个汉字就是乱码.请问是怎么回事.
网友回复:写一个过滤器。

Java code




Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



package what;



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;



public class gl implements Filter {



public void init(FilterConfig arg0) throws ServletException {



}



public void doFilter(ServletRequest arg0, ServletResponse arg1,

FilterChain arg2) throws IOException, ServletException {

arg0.setCharacterEncoding("GBK");

arg1.setCharacterEncoding("GBK");

arg2.doFilter(arg0, arg1);

}



public void destroy() {



}



}


在XML里配置下
XML code




Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





gl

what.gl





gl

/*