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属性值是否设置对

 

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

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

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

web.xml


    
        Utf_8Filter
        你写的过滤器的全路径,包名+filter类名
        
            encoding
            utf-8
        
    
    
        Utf_8Filter
        /*
    

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,记得要把环境变量也重新配置一下。

 

你可能感兴趣的:(异常错误)