jdbc连接sql server 中文乱码,为解决这个问题,我写了一个类,但是没用
我写了个类,然后再web.xml配置了一下
package helper;
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.http.HttpServletRequest;
public class UTF8Filter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws
IOException,
ServletException {
((HttpServletRequest)request).setCharacterEncoding("utf-8");
((HttpServletRequest)request).setCharacterEncoding("utf-8");
chain.doFilter(request, response);
System.out.println("hhhhhh");
}
public void init(FilterConfig arg0) throws ServletException {
}
}
下边的是我的配置
<filter>
<filter-name>UTF8Filter</filter-name>
<filter-class>helper.UTF8Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>UTF8Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
JDBC
SQL?Server
乱码
Filter
UTF-8
------解决方案--------------------Java连接sql server很少出现乱码,你的应该是前台传递过来时出现了乱码,然后将乱码存储到了数据库中。
产生乱码主要是编码不统一,如果如果统一了编码,还有说明没有人为的去解决乱码。
楼主来看看我的博客吧!应该会对你有用。
http://blog.csdn.net/long2010yu2010/article/details/7856270
------解决方案--------------------你过滤的都是request,当然错了。改为
public class UTF8Filter extends HttpServlet implements Filter
{
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)arg0;
request.setCharacterEncoding("UTF-8");
HttpServletResponse response = (HttpServletResponse)arg1;
response.setContentType("text/html;charset=utf-8");
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException
{
}
}