mysql與jsp亂碼解決方法

terryisme發表於2009-03-05

在資料庫連線時新增:Connection Conn=DriverManager.getConnection("jdbc:mysql://localhost /test?useUnicode=true&characterEncoding=gb2312(或utf-8)","root","");

將mysql中的filed charset 屬性設定為gbk.

新增類SetEncodingFilter

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;


/** *//**
* 請求中中文字串過濾類
*/

public class SetEncodingFilter
implements Filter{


public void init(FilterConfig filterConfig) throws ServletException {
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

request.setCharacterEncoding("gb2312");
chain.doFilter(request,response);
}

public void destroy(){
}
}

配置web.xml




action
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml


debug
3


detail
3

0


action
*.do


index.jsp


Set Encoding
com.blog.filter.SetEncodingFilter


Set Encoding
/*

[@more@]%CC%EC%D1%C4%D2%BB%D0%A6/blog/item/b8e7f3092d175a87d0581bea.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/93029/viewspace-1018124/,如需轉載,請註明出處,否則將追究法律責任。

相關文章