成长中的记忆
这是我的成长,这是我的天地,学习JAVA,只因快乐。
posts - 4,comments - 16,trackbacks - 0

做JSP程序时页面传的文字经常是乱码,如何解决呢?

一种方法是把tomcat中所有的<Connector>标签的URIEncoding属性进行设置,如: <Connector port="8000" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />

而另一种方法就是写过滤器

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class EncodingFilter implements Filter {
    FilterConfig config = null;
    // default to GBK
    private String targetEncoding = "GBK";

    public void init(FilterConfig config) throws ServletException {
        this.config = config;
        this.targetEncoding = config.getInitParameter("encoding");
    }

    public void destroy() {
        config = null;
        targetEncoding = null;
    }

    public void doFilter(ServletRequest srequest, ServletResponse sresponse,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest)srequest;
        request.setCharacterEncoding(targetEncoding);
        chain.doFilter(srequest, sresponse);
    }
}

配置:在web.xml中添加
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>xx.xx.xx.EncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>Shift_JIS</param-value>
        </init-param>
        <init-param>
            <param-name>useragents</param-name>
            <param-value>Mac</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

至于来自何处,忘记了。嘿嘿。。。

posted on 2006-02-17 16:06 aiyoyoyo 阅读(3993) 评论(1)  编辑  收藏 所属分类: JSP

FeedBack:
# re: JSP乱码解决(过虑器EncodingFilte11111
2014-03-19 08:53 | 1111
11111  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: