谢谢大家,用你们的方法,我已经解决了.我试了下,可以再编写一个javabean,专门来进行字符编码转换的,可以在JSP文件中,调用JAVABEAN文件,不过适合JSP文件较多的时候.
编码转换不是调用JAVABEAN去实现,而是用过滤器实现.我这里给你完整代码!
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 {
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{
request.setCharacterEncoding("GB2312");
chain.doFilter(request,response);
}
public void init(FilterConfig filterConfig)throws ServletException{}
public void destroy() {
// todo:
}
}
web.xml配置方法:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee"
"http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>mywork.util.SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>