解决struts开发中文乱码

  1. 1.整体思想,编码方式统一(采用utf-8)

2页面显示中文乱码

<%page pageEncoding="utf-8"%>

3传递中文参数乱码

修改server.xml(解决get方式)

<Connector port="8080" protocol="HTTP/1.1" maxThreads="150" connectionTimeout="20000"

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

编写过滤器(Filter)(解决post方式)

  1. packageservlets;
  2. importjava.io.IOException;
  3. importjava.io.IOException;
  4. importjavax.servlet.Filter;
  5. importjavax.servlet.FilterChain;
  6. importjavax.servlet.FilterConfig;
  7. importjavax.servlet.ServletException;
  8. importjavax.servlet.ServletRequest;
  9. importjavax.servlet.ServletResponse;
  10. publicclassEncodingFilterimplementsFilter{
  11. publicvoidinit(FilterConfigconfig)throwsServletException{
  12. }
  13. publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,
  14. FilterChainchain)throwsIOException,ServletException{
  15. request.setCharacterEncoding("utf-8");
  16. chain.doFilter(request,response);
  17. }
  18. publicvoiddestroy(){
  19. }
  20. }

在web.xml注册

  1. <filter>
  2. <filter-name>EncodingFilter</filter-name>
  3. <filter-class>servlets.EncodingFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>EncodingFilter</filter-name>
  7. <url-pattern>*.do</url-pattern>
  8. </filter-mapping>

你可能感兴趣的:(struts)