JSP HTML 各种 乱码 解决方法|jsp include html乱码|include 乱码|MyEclipse 中文乱码

笔者花了一整天研究这个问题 。最终解决了所有的中文乱码问题。

不用 写 过滤器,不用改 tomcat 的配置文件

 

笔者使用的 软件是  MyEclipse2013 professional 版

 

JSP 文件 include html 乱码

描述:运行时  JSP本身的中文部分 不乱码,include 的html 的那一部分  的中文乱码

   <%@include file="xxxx.html"%>

 

解决方法:

 

把xxxx.html 用 Notepad++  执行   格式 -> 转为 UTF-8编码

然后把 jsp 文件什么的都转成  UTF-8编码格式 保存

xxx.java文件不用改

 

JSP的前面这么写

<%@page contentType="text/html;charSet=UTF-8"%>
<%@page pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8");%>
<%response.setCharacterEncoding("UTF-8");%>



即可解决各种地方的中文乱码

如果  java 处理请求,取  value 之类的乱码,例如

 

String []items=cart.getItems();
     for(int i=0;i<items.length;i++)
     {   
    %>
    <li><%=items[i] %></li>



这样 中文依然乱码 

 

那么在  for 里面  加上

 

String items_zh=new String(items[i].getBytes("iso-8859-1"), "UTF-8");

我试过很多种编码 只有这个中文没有乱码

然后把<%=items[i] %>改为<%=items_zh %>就没有任何问题了。

 

 

这种情况也行

如果 html 有中文 ,其他的都是ANSI (记事本编辑的),那么就是GBK编码了,只需要改 html 的编码为UTF-8

<%@page contentType="text/html;charSet=GB2312"%>
<%@page pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
<%response.setCharacterEncoding("GB2312");%>

以及

String items_zh=new String(items[i].getBytes("iso-8859-1"), "GB2312");

然后其他的内容不用更改

原文地址:https://www.cnblogs.com/slankka/p/9158594.html