SSH传参中文乱码问题解决方法

   自己编写filter

public class CharsetEncodingFilter implements Filter {

private String charsetEncoding = null;

private String enable = null;

public void destroy() {

this.charsetEncoding = null;

this.enable = null;

}


public void doFilter(ServletRequest request, ServletResponse response,

FilterChain arg2) throws IOException, ServletException {

if(this.enable.equals("true")) {

request.setCharacterEncoding(charsetEncoding);

response.setContentType("text/html;charset=" + charsetEncoding);

arg2.doFilter(request, response);

}

}


public void init(FilterConfig config) throws ServletException {

this.charsetEncoding = config.getInitParameter("CharsetEncoding");

this.enable = config.getInitParameter("enable");

}

}

说明:上面的encoding根据你用的编码类型决定,在这里我用utf-8

  web.xml中配置上面的filter

<filter>

        <filter-name>charFilter</filter-name>

        <filter-class>com.landtofrest.util.EncodingFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>charFilter</filter-name>

        <url-pattern>/*</url-pattern>

</filter-mapping>

说明:<filter-class>是上面filter类的路径:包.类名

      <filter-name>命名随便

      直接在web.xml配置如下:

<filter>

<filter-name>SpringCharacterEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>SpringCharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

  tomcat中配置server.xml,我的是MYEclipse自带的。我在myEclipse里面找不到tomcatserver.xml。而是在workspace->.metadata->.me_tcat->conf->server.xml那里找到

修改:

 <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" URIEncoding="UTF-8"/>

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>

 

说明:红色部分是要添上去的

 

  应该可以运行了!?页面参数传到action在控制台输出已经不是乱码,可是数据库查询还是查不到记录,后面查了很多资料,才知道,mysql中编码也是有讲究的。

找到mysql安装目录,打开my.ini文档,把这句default-character-set=Latin1改成这样子default-character-set=utf8。注意是default-character-set有两句哟。

 

说明:如果在建表时下面的第⑤步已经做了的话,第④步是没有必要滴。

如果已经做了第④步,第⑤步中建表时有没有必要在指定引擎后再添加DEFAULT CHARSET =utf8,这个我还没有试验

 

  应该可以运行了!?还是不行。

因为我在mysql用命令【show create table 表名;】查看自己在建表的时候默认的是Latin1,所以得改过来,建表时应该【create 表名()ENGINE=引擎 DEFAULT CHARSET =utf8;】。

或者这样比较麻烦,可以有另一种办法就是在建表之前用【set names utf8;】命令,这样在关闭mysql之前都用utf8而不用再在建每个表都是这样。

   你会发现如果用【set names utf8;】而没改的话,用insert插入数据时就出现data too long的错误,所以,用想要插入得用【set names gb2312或者gbk;】,习惯用gbk,这样就可以插入数据了。

转载于:https://my.oschina.net/u/553734/blog/59909

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值