首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JSP网页里是charset=UTF-8,Mysql5里存取的数据库的charset也设成了UTF-8,可为什么通过JSP网页存取中文还是显示乱码

2011-12-17 
JSP网页里是charsetUTF-8,Mysql5里存取的数据库的charset也设成了UTF-8,可为啥通过JSP网页存取中文还是显

JSP网页里是charset=UTF-8,Mysql5里存取的数据库的charset也设成了UTF-8,可为啥通过JSP网页存取中文还是显示乱码?
JSP网页里是charset=UTF-8:
<%@   page   contentType= "text/html;   charset=UTF-8 "   language= "java "   import= "java.sql.* "   errorPage= "error.jsp "   %>

Mysql5里存取的数据库的charset也设成了UTF-8,可为啥通过JSP网页存取中文还是显示乱码?

还需要在Mysql5里添加啥设置,或在JSP网页里添加啥语句吗?


[解决办法]
从数据库中读出后再编码..
new String(xxx.getBytes( "ISO8859_1 "), "GBK ")
[解决办法]
$charset = 'utf8 ';
mysql_query( "SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary ");
[解决办法]

[解决办法]
上月也碰到这个问题,俺的解决方法是这样,楼主试看:
当时的环境是:装了中文语言包的英文2003 + (因为是给客户的服务器上安装,有个不知道为什么会不认gbk字符集的)中文JRE1.4 + Mysql5 +俺公司做的java后台服务

1)先用sql语句(帮助文档里面有)查数据库和表里面的字符集,然后通通用sql语句改成gb2312的;
2)java -version,看它有没冒出来说不支持这个字符集,如果冒了,就把它卸了重装;

然后搞定。。。
[解决办法]
1.添加filter过滤 注明encoding为 UTF-8

2.jsp页面 <%@ page language= "java " pageEncoding= "UTF-8 "%>

3.mysql安装时选择utf-8 ,建库的时候也要注明utf-8 编码(sql语句中)
[解决办法]
必解决中文问题
1.在Tomcat 的server.xml 中

<Connector port= "8080 " maxHttpHeaderSize= "8192 "
maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
connectionTimeout= "20000 " disableUploadTimeout= "true " URIEncoding= "gbk " />

2.Filter
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app version= "2.4 "
xmlns= "http://java.sun.com/xml/ns/j2ee "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<filter>
<filter-name> setCharacterEncodingFilter </filter-name>
<filter-class> asp.SetCharacterEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> GBK </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> setCharacterEncodingFilter </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
</web-app>

package asp;
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 SetCharacterEncodingFilter implements Filter
{
/**
* Example filter that sets the character encoding to be used in parsing the incoming request
*/
/**
* Take this filter out of service.
*/
public void destroy()
{
}
/**
* Select and set (if specified) the character encoding to be used to interpret request parameters for this request.


*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
// response.setCharacterEncoding( "UTF-8 ");
request.setCharacterEncoding( "GB18030 ");
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException
{
}
}
3.每个页面

<%@ page contentType= "text/html; charset=gb2312 " language= "java " errorPage= " " %>

一切就OK..
如果用Ajax
那么.在保证前三个部份的前题下.

get 不变
post 使用两次encodeURI
function createPostDo(/*条件*/,actionUrl)/*Ajax post 第二步 将Post的条件格式发送到指定的URL*/
{
post = encodeURI(/*条件*/);
post = encodeURI((/*条件*/);//两次,很关键
var xmlObj = new ActiveXObject( 'Msxml2.XMLHTTP ');
var URL = actionUrl;//文件名需要调整成测试时的相应位置?
xmlObj.open ( 'post ',URL,false);
xmlObj.setrequestheader( "cache-control ", "no-cache ");
xmlObj.setrequestheader( "Content-Type ", "application/x-www-form-urlencoded ");
xmlObj.send (post);//注意:POST方式,使用这个来发送内容?
}

热点排行