我采用的SQL server2000数据库 但是添加信息到数据库中显示中文乱码?求助给位了 多谢先!
解决方案 »
- 结果集 ResultSet rs中 如何得到char 没有rs.getChar方法
- toggle问题寻求帮助
- 关于java web 的学习
- Struts2 select 问题
- 急!!WEB更新的奇怪的问题。。。。
- 用了两天解决不了,求救:点表单中的计算按钮后刷新页面,如何在刷新页面后使滚动条停在固定位置?
- 初学者遇到个问题,请大哥们帮忙调试下
- 在JSP页面中,能否嵌入ActiveX控件
- 你没遇到java的中文问题吗?那你遇到过jsp的中文问题吗?你用什么数据库?
- Java向MySQL数据库中插入信息,执行一次会插入两次,这怎么解决?
- 我在servlet中把一个从数据库中抽出的数据集放在了一个request对象里,在jsp中怎么取出阿
- int a=Integer.parseInt(itemcount);有问题?
楼主【juchao】截止到2008-06-23 22:41:13的历史汇总数据(不包括此帖):
发帖数:30 发帖分:1822
结贴数:22 结贴分:1242
未结数:8 未结分:580
结贴率:73.33 % 结分率:68.17 %
楼主加油
2.可以设置tomcat的uri encoding
个人认为是数据库字符编码设置问题
头部加上 <%@ page contentType="text/html; charset=GB2312" %> 提交页面的文件中加入 request.setCharacterEncoding("GBK");
因为sqlserver2000数据库已经解决了中文乱码问题.
所以你的乱码应该是在存之前已经是乱码了.
可以将页面
<%@ page contentType="text/html; charset=GB2312" pageEncoding="gbk2312"%>
在针对性地处理页面乱码就OK了.
你按以下方式对得到的值进行编码试一下
String userName = request.getParameter("username");
userName = new String(userName.getBytes("8859_1"),"gb2312");
这里有一些相关的问题,你看一下吧,
JSP中:
<%@ page contentType="text/html; charset=***" %>servlet或filter中:
request.setCharacterEncoding("***");
response.setContentType("text/html; charset=***");
SQL server2000中:
也用相同的字符集***
变量=(new String(添加的信息.getBytes("iso-8859-1"),"gb2312")
或变量=(new String(添加的信息.getBytes("iso-8859-1"),"gb2312")
第一加一个过滤器
public class ChineseFilter implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
//String encoding=filterConfig.getInitParameter("enconing");//也可动态从WEB.xml配置文件中取出参数,这样我们可以通过配置修改编码格式.
request.setCharacterEncoding("gbk");//设置请求的编码格式
response.setCharacterEncoding("gbk");
filterChain.doFilter(request, response);
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
public void destroy() {
}第二,如果你是用的tomcat就在server.xml文件<Connector>这个节点中加上URIEncoding="gbk"
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;
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 {// 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);}
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);}
}
3、在web.xml中增加下面代码:<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>4、编写JSP文件,在文件头部增加代码:<%@ page pageEncoding="GBK"%>或者<%@ page contentType="text/html;charset=GBK"%>根据以上步骤,重新启动Tomcat服务,即可正确显示中文
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;
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 {// 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);}
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);}
}
3、在web.xml中增加下面代码:<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>4、编写JSP文件,在文件头部增加代码:<%@ page pageEncoding="GBK"%>或者<%@ page contentType="text/html;charset=GBK"%>根据以上步骤,重新启动Tomcat服务,即可正确显示中文
你存放中文信息的栏位的需要以Unicode的方式存放
...INSERT INTO table values(N'中文' ......)