struts,Oracle 数据库 乱码解决方法
来源:互联网 发布:sql语言具有什么功能 编辑:程序博客网 时间:2024/05/20 22:27
1.Struts 乱码问题的解决。
解决的方法只有一个,设置request.setCharacterEncoding(encoding);
处理方式有很多种,这儿列举3种。
1.1 直接转换
1.2 使用过滤器 (这种比较好)
1.1 直接转换
String name1=new String(name.getBytes("iso-8859-1"),"gb2312");
不是很好,如果参数太多,转换代码就很多
1.2 使用过滤器
请参照tomcat 实例中【webapps/jsp-examples/WEB-INF/classes/filters】SetCharacterEncodingFilter.java类,和【/jsp-examples/WEB-INF】中web.xml的设置,
SetCharacterEncodingFilter.java 可以不用修改。
web.xml 中设置为你的转换格式。
eg:
SetCharacterEncodingFilter.java
package com.hz;
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);
}
}
web.xml 中的设置
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.hz.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
往数据库中添加数据出现乱码,我是因为数据库编码和程序编码不一样引起的。
总结:
JSP,Struts 乱码,采用过滤器,网上查了很多资料,结果都不正确,后来参照Tomcat的实例,一下就解决了。所以,网上的资料虽多,很多都没有用,采用什么技术,直接去开发技术的官网,找相应的解决方法。或参照相应正确的实例, 往往更快。
解决的方法只有一个,设置request.setCharacterEncoding(encoding);
处理方式有很多种,这儿列举3种。
1.1 直接转换
1.2 使用过滤器 (这种比较好)
1.1 直接转换
String name1=new String(name.getBytes("iso-8859-1"),"gb2312");
不是很好,如果参数太多,转换代码就很多
1.2 使用过滤器
请参照tomcat 实例中【webapps/jsp-examples/WEB-INF/classes/filters】SetCharacterEncodingFilter.java类,和【/jsp-examples/WEB-INF】中web.xml的设置,
SetCharacterEncodingFilter.java 可以不用修改。
web.xml 中设置为你的转换格式。
eg:
SetCharacterEncodingFilter.java
package com.hz;
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);
}
}
web.xml 中的设置
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.hz.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
往数据库中添加数据出现乱码,我是因为数据库编码和程序编码不一样引起的。
总结:
JSP,Struts 乱码,采用过滤器,网上查了很多资料,结果都不正确,后来参照Tomcat的实例,一下就解决了。所以,网上的资料虽多,很多都没有用,采用什么技术,直接去开发技术的官网,找相应的解决方法。或参照相应正确的实例, 往往更快。
- struts,Oracle 数据库 乱码解决方法
- struts,Oracle 数据库 乱码解决方法
- Oracle数据库中文乱码解决方法
- struts中文乱码解决方法
- struts中文乱码解决方法
- Struts国际化乱码解决方法- -
- struts中文乱码解决方法详解
- struts表单乱码的解决方法
- ASP.NET连接ORACLE数据库出现乱码的解决方法
- oracle数据库复制工具exp、imp 乱码问题与解决方法
- Oracle 客户端乱码解决方法
- Oracle乱码解决方法
- mysql数据库乱码解决方法
- Mysql数据库乱码解决方法
- oracle 数据库乱码解决方案
- Oracle 数据库中文乱码
- oracle数据库乱码问题解决
- oracle数据库乱码
- The First MFC Application
- List Control控件技巧总汇
- 5mb内存搞定windows系统安全问题
- C++操作符重载专题(C++)
- 初次用spring之感受
- struts,Oracle 数据库 乱码解决方法
- gfdgfdg
- 离开vs2005使用VS2008的充分理由
- repeater 分页问题讨论
- 操作系统银行家算法.NETC#编写(处理机调度与死锁)银行家算法
- 如何在stdin(键盘)中输入EOF呢?
- 打磨Comodo,做有意义的事儿(1)
- VB中FSO的调用方法
- sqlcommand的应用