JSP中文问题----不知道是哪里出的问题
mysql 的 Encoding 是 gb2312
页面的设置也是 <%@ page contentType= "text/html; charset=gb2312 " %>
按理说表单传过来的中文,经过
String test=new String(request.getParameter( "aa ").getBytes( "ISO8859_1 "));
转换,就应该没问题了啊,但是还是显示 "??? ",真不知道该怎么办了?到底是哪里出了问题呢??
我用的是tomcat5.5 JDK1.5
[解决办法]
String test=new String(request.getParameter( "aa ").getBytes( "ISO8859_1 "), "GB2312 ");
[解决办法]
同意樓上的說法。。
[解决办法]
在tomcat的server.xml中,加入URIEncoding= "Windows-31J
只能解决用get提交,
用post的话,就要用 <% request.setCharacterEncoding( "Windows-31J "); %>
new String(request.getParameter( "username ").getBytes( "ISO_8859_1 "), "GBK ");就都可以解决了,因为tomcat内部是ISO_8859_1,不过上面的两中方式就不要加了
三种方式是独立的
[解决办法]
接受页面:request.setCharacterEncoding( "utf-8 ");
发送页面:response.setCharacterEncoding( "GBK ");
[解决办法]
乱码阿,乱码...怎么天天有人问阿...
加上&characterEncoding=gb2312式式
*******************************
环境:
WindowsXP中文
Eclipse3.2.1+Myeclipse5.1.0GA
Tomcat5.5
JDK1.5.0
Hibernate3.1
Mysql5.0+ mysql-connector-java-5.0.4-bin.jar
方案:
1.集成开发环境Eclipse中设置文本文件存储编码为UTF-8。
//我想是因为….如果所做工程项目最终要在别的版本操作系统的服务器上跑,这里需要设置(未经证实)
2.数据库mysql,默认编码使用utf8;
并且创建数据库时在语句后面追加DEFAULT CHARSET=utf8;set names utf8;
//如果数据库默认编码是utf8,那这个也不是必需的吧(未经证实)
3.跟数据库连接的URL:Hibernate URL:
jdbc:mysql://127.0.0.1:3306/addressbook?useUnicode=true&characterEncoding=utf8
//如果数据库默认编码是utf8,那这个也不是必需的吧(未经证实)
4.使用过滤器,过滤器文件代码见后面附1。
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> org.biti.filters.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>
//过滤的是post还是get还没弄明白,据说只过滤器中一个,另一个见5。
5.修改Tomcat配置文件server.xml中Connector部分
<Connector port= "80 "
maxHttpHeaderSize= "8192 "
maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
connectionTimeout= "20000 " disableUploadTimeout= "true " />
加入URIEncoding= "UTF-8 "一项。
//我现在这个没乱码的就没有设置….
附1:SetCharacterEncodingFilter.java(可在Tomcat示例源码中找到)
package org.biti.filters;
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 {
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);
}
}
[解决办法]
上面的楼主说得是对的