java项目解决乱码

设置UTF-8:

1.查看eclipse的编码设置是否有问题,

1.windows——>Preferences——>Web——>JSP Files设置为UTF-8

2.windows——>Preferences——>General——>Workspace 设置为UTF-8

3.右键项目——>Properties——>Resource 设置为UTF-8

2.如果还未解决,就看下你的html和jsp文件中charset属性值是否设置对

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

3.还不行就去修改tomcat的server.xml,找到下面这行代码,添加 URIEncoding="UTF-8"

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"  URIEncoding="UTF-8"/> 

4.如果你还未解决,那就加一个过滤器filter设置编码格式为UTF-8

web.xml

<!-- 编码过滤器 -->
    <filter>
        <filter-name>Utf_8Filter</filter-name>
        <filter-class>你写的过滤器的全路径,包名+filter类名</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Utf_8Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

filter类

public class SetEncodingFilter implements Filter { //要实现Filter接口
 //存储编码格式信息
 private String encoding = null;
 public void destroy(){
 }
 public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain)
   throws IOException,ServletException{
  //转换
  HttpServletRequest request = (HttpServletRequest)req;
  HttpServletResponse response = (HttpServletResponse)resp;
  /*
   * 判断在web.xml文件中是否配置了编码格式的信息
   * 如果为空,则设置编码格式为配置文件中的编码格式
   * 否则编码格式设置为GBK
   */
  if(this.encoding != null && !this.encoding.equals("")){
   request.setCharacterEncoding(this.encoding);
   response.setCharacterEncoding(this.encoding);
  }else{
   request.setCharacterEncoding("UTF-8");
   response.setCharacterEncoding("UTF-8");
  }
  /*
   * 使用doFilter方法调用链中的下一个过滤器或目标资源(servlet或JSP页面)。
   * chain.doFilter处理过滤器的其余部分(如果有的话),最终处理请求的servlet或JSP页面。
   */
  chain.doFilter(request, response);
 }
 public void init(FilterConfig config) throws ServletException{
  //获取在web.xml文件中配置了的编码格式的信息
  this.encode = config.getInitParameter("encoding");
 }
}

 5.劝你换一下你的tomcat,记得要把环境变量也重新配置一下。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值