当前位置: 代码迷 >> Java Web开发 >> struts连接sqlserver 2000,插入数据乱码有关问题
  详细解决方案

struts连接sqlserver 2000,插入数据乱码有关问题

热度:95   发布时间:2016-04-17 15:27:17.0
struts连接sqlserver 2000,插入数据乱码问题
我有一个注册页regist.jsp,业务逻辑user.java(实现用户查询,更新等操作),控制处理层有registForm.java(得到注册的数据),registAction.java(主要是得到注册数据,并用user.java的的方法实现更新操作)。现在出现的问题是:写入到sqlserver中的数据是汉字的都出现了??,给看看怎么回事,谢谢

------解决方案--------------------
做一个Filter,专门处理request时的编码处理
------解决方案--------------------
用filter解决
------解决方案--------------------
1.web.xml中加入过滤器
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> com.test.filters.SetCharacterEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> UTF-8 </param-value>
</init-param>
</filter>
---------------------------------------
package com.test.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;
//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);

}

}
  相关解决方案