JSP中文问题
怎么添加到数据库都是乱码
怎么解细
<%!
String name,price,spec,ku_count,remark;
ResultSet rs=null;
%>
<body>
<%
//wu_name=request.getParameter( "wu_name ").trim();
name=request.getParameter( "name ");
out.println(name);
price=request.getParameter( "price ");
out.println(price);
spec=request.getParameter( "spec ");
out.println(spec);
ku_count=request.getParameter( "ku_count ");
out.println(ku_count);
remark=request.getParameter( "remark ");
out.println(remark);
p_dbBean.executeUpdate( "insert into depot values( ' "+name+ " ', ' "+price+ " ', ' "+spec+ " ', ' "+ku_count+ " ', ' "+remark+ " ',getdate()) ");
[解决办法]
(1)先看看取过来的参数是不是乱码
(2)数据库的字段属性是否设置有问题
(3)使用的是哪中数据库~~好象MYSQL有这个问题
以上谨供参考~
[解决办法]
1.编码问题:
(1)首先确定JSP页面头部是否有: <%@ page contentType= "text/html; charset=GBK " %>
(2)类似这样的转码:
String param= new String(request.getParameter( "param ").getBytes( "ISO-8859-1 "), "GBK ");
(3)添加filter字符过滤器,具体做法:
先添加类:
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;
import org.apache.log4j.Logger;
import java.net.URLEncoder;
/** *//**
* 请求中中文字符串过滤类
*/
public class SetEncodingFilter
implements Filter ...{
private Logger logger=Logger.getLogger(this.getClass());
public void init(FilterConfig filterConfig) throws ServletException ...{
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException ...{
logger.info( "请求转码过滤器=================== ");
request.setCharacterEncoding( "gb2312 ");
chain.doFilter(request,response);
}
public void destroy() ...{
}
}
再注册类到XML里:
<filter>
<filter-name> Set Encoding </filter-name>
<filter-class> SetEncodingFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> Set Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
(4)如果是通过 "a.jsp?param=中文 "传递参数,则需要:
a.在传参数之前先把参数进行转码:java.net.URLEncoder.encode(param);
取值用java.net.URLDncoder.dncode(param);再转回中文
b.在你的Tomcat目录--> conf目录--> server.xml里找出这段:
<Connector
port= "8080 " maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
debug= "0 " connectionTimeout= "20000 "
disableUploadTimeout= "true " <!--在里边加上这个参数--> URIEncoding= "gb2312 "
/>
(5)数据库乱码,首先确定你在插入数据库之前是中文,解决办法:
在数据库的数据库连接URL中加上useUnicode=true&characterEncoding=GBK就OK了。
如果在读取的时候出现乱码用第二种方法解决.
http://blog.csdn.net/lip009/archive/2006/09/07/1192022.aspx#521755
[解决办法]
先检查你数据库表的默认字符集,如果你是Tomcat的话
http://www.blogjava.net/richardeee/archive/2007/01/23/Chinese_encoding_problem_for_all.html
[解决办法]
在开头加上:
<%@ page contentType= "text/html; charset=GBK " %>
<%
response.setCharacterEncoding( "GBK ");
%>
[解决办法]
楼上的好象不行,这个好象只是在本页面显示中文才能,上面的好象已经有正解解决方案了~~