打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java 解决字符集的乱码问题

struts1中文乱码解决办法

1.首先jsp页面要使用UTF-8编码,个人建议将pageEncodingcontentType中的编码全部设置为UTF-8

2.修改tomcat配置文件server.xml,添加一个属性[红色部分],如下

       <Connector
               port="8080"               maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />

3.编写过滤器类

Java代码

1.      public class SetCharacterEncodingFilter implements Filter {   

2.        

3.          protected String encoding = null;   

4.        

5.          protected FilterConfig filterConfig = null;   

6.        

7.          protected boolean ignore = true;   

8.        

9.          /*  

10.        * (non-Javadoc)  

11.        *   

12.        * @see javax.servlet.Filter#destroy()  

13.        */  

14.       public void destroy() {   

15.     

16.           this.encoding = null;   

17.           this.filterConfig = null;   

18.     

19.       }   

20.     

21.       /*  

22.        * (non-Javadoc)  

23.        *   

24.        * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,  

25.        *      javax.servlet.ServletResponse, javax.servlet.FilterChain)  

26.        */  

27.       public void doFilter(ServletRequest request, ServletResponse response,   

28.               FilterChain chain) throws IOException, ServletException {   

29.     

30.           // Conditionally select and set the character encoding to be used   

31.           if (ignore || (request.getCharacterEncoding() == null)) {   

32.               String encoding = selectEncoding(request);   

33.               if (encoding != null)   

34.                   request.setCharacterEncoding(encoding);   

35.           }   

36.     

37.           // Pass control on to the next filter   

38.           chain.doFilter(request, response);   

39.       }   

40.     

41.       /*  

42.        * (non-Javadoc)  

43.        *   

44.        * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)  

45.        */  

46.       public void init(FilterConfig filterConfig) throws ServletException {   

47.     

48.           this.filterConfig = filterConfig;   

49.           this.encoding = filterConfig.getInitParameter("encoding");   

50.           String value = filterConfig.getInitParameter("ignore");   

51.           if (value == null)   

52.               this.ignore = true;   

53.           else if (value.equalsIgnoreCase("true"))   

54.               this.ignore = true;   

55.           else if (value.equalsIgnoreCase("yes"))   

56.               this.ignore = true;   

57.           else  

58.               this.ignore = false;   

59.     

60.       }   

61.     

62.       /*  

63.        *   

64.        */  

65.       protected String selectEncoding(ServletRequest request) {   

66.     

67.           return (this.encoding);   

68.     

69.       }   

70.     

71.   }  

public class SetCharacterEncodingFilter implements Filter {

 

protected String encoding = null;

 

protected FilterConfig filterConfig = null;

 

protected boolean ignore = true;

 

/*

 * (non-Javadoc)

 *

 * @see javax.servlet.Filter#destroy()

 */

public void destroy() {

 

  this.encoding = null;

  this.filterConfig = null;

 

}

 

/*

 * (non-Javadoc)

 *

 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,

 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)

 */

public void doFilter(ServletRequest request, ServletResponse response,

   FilterChain chain) throws IOException, ServletException {

 

  // Conditionally select and set the character encoding to be used

  if (ignore || (request.getCharacterEncoding() == null)) {

   String encoding = selectEncoding(request);

   if (encoding != null)

    request.setCharacterEncoding(encoding);

  }

 

  // Pass control on to the next filter

  chain.doFilter(request, response);

}

 

/*

 * (non-Javadoc)

 *

 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)

 */

public void init(FilterConfig filterConfig) throws ServletException {

 

  this.filterConfig = filterConfig;

  this.encoding = filterConfig.getInitParameter("encoding");

  String value = filterConfig.getInitParameter("ignore");

  if (value == null)

   this.ignore = true;

  else if (value.equalsIgnoreCase("true"))

   this.ignore = true;

  else if (value.equalsIgnoreCase("yes"))

   this.ignore = true;

  else

   this.ignore = false;

 

}

 

/*

 *

 */

protected String selectEncoding(ServletRequest request) {

 

  return (this.encoding);

 

}

 

}

 4.web.xml中添加下列代码

 

Xml代码

1.      <filter>  

2.              <filter-name>Encoding</filter-name>  

3.              <filter-class>  

4.                  struts.util.SetCharacterEncodingFilter   

5.              </filter-class>  

6.              <init-param>  

7.                  <param-name>encoding</param-name>  

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

9.              </init-param>  

10.       </filter>  

11.       <filter-mapping>  

12.           <filter-name>Encoding</filter-name>  

13.           <url-pattern>/*</url-pattern>  

14.       </filter-mapping>  

看了一下楼主链接中的filter代码,应该没问题的。

如果不加filter,页面采用utf-8,的确会在java代码中出现中文乱码的情况。

然后我就写了一个filter,大致跟链接中的内容一致,然后就解决了乱码问题。

我贴一下我的代码吧

web.xml代码

<filter>

   <filter-name>charactarFileter</filter-name>

   <filter-class>com.yourcompany.struts.CharactarEncodingFilter</filter-class>

  </filter>

  <filter-mapping>

   <filter-name>charactarFileter</filter-name>

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

  </filter-mapping>

filter实现代码

package com.yourcompany.struts;

 

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 CharactarEncodingFilter implements Filter{

 

 public void destroy() {

 }

 

 public void doFilter(ServletRequest request, ServletResponse response,

   FilterChain chain) throws IOException, ServletException {

  request.setCharacterEncoding("utf-8");

  chain.doFilter(request, response);

 }

 

 public void init(FilterConfig arg0) throws ServletException {

 }

 

}

就这样,写好后重新发布项目,运行,解决问题。

 

 

 

 

 

1.修改Tomcatconf/server.xml文件中 <Connector
port="8080" ...
处添加URIEncoding="UTF-8"

2.
编写一个过滤器类CharacterEncodingFilter

package com.lyb.example;

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 CharacterEncodingFilter implements Filter {

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request,response);
// TODO Auto-generated method stub

}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}


3.
web.xml中添加过滤器映射
<filter>
  <filter-name>characterEncoding</filter-name>
  <filter-class>com.lyb.example.CharacterEncodingFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>characterEncoding</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

OK
。重新启动Tomcat即可

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Filter技术的应用(转)
Mysql与JSP网页中文乱码问题的解决方案
Servlet中文乱码解决
几个有用的Servlet过滤器
Servlet过滤器和监听器知识总结
一个servlet登陆过滤器
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服