jsp include html 乱码问题 <%@ include page=""%>

本文探讨了在使用JSP的&lt;%@include page%&gt;指令包含HTML页面时遇到的中文乱码问题。通过调整pageEncoding属性,确保了包含文件与被包含文件编码一致,从而解决了乱码问题。

在使用<%@ include page=""%> 指令包含一个html页面(b.html)时中文乱码。  乱码问题 首先想到的就是这两个文件的编码是否统一

在仔细检查了一遍后 发现没问题 都是UTF-8的。 既能编码一致  那为什么还好出现乱码呢。

  于是就把html的内容重新放到一个jsp文件(c.jsp)中去 inlude 该jsp文件 没无问题   正常 显示  那是啥问题了 ⊙﹏⊙b汗

也是 仔细比较了b.html 和c.jsp 文件  发现这两个文件 除了meta标签里面的内容不同的话 就是 c.jsp 文件比b.html 多了行代码 

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

是不是和这有关呢    把这复制到b.html 页面中 刷新 中文正常显示了 Ooo 


问题是解决了 但是为啥呢  得仔细分析分析 <%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%> 这行代码了

这行代码中有两个指定编码格式的指令 charset 和 pageEncoding 其中 html页面的<meta>中已经有了 charset 所以 这行代码可以简写为

<%@ page language="java"  pageEncoding="UTF-8"%> 


下面我们分析下 pageEncoding和contentType两种属性的区别:

1)pageEncoding是jsp文件本身的编码
2)contentType的charset是指服务器发送给客户端时的内容编码

jsp 从用户发送请求到相应给用户中间要经历三个阶段

一:jsp编译成.java,它会根据pageEncoding的设定读取jsp,结果是由指定的编码方案翻译成统一的UTF-8 JAVA源码(即.java),如果pageEncoding设定错了,或没有设定,出来的就是中文乱码。

二:是由JAVAC的JAVA源码至java byteCode的编译,不论JSP编写时候用的是什么编码方案,经过这个阶段的结果全部是UTF-8的encoding的java源码。
JAVAC用UTF-8的encoding读取java源码,编译成UTF-8 encoding的二进制码(即.class),这是JVM对常数字串在二进制码(java encoding)内表达的规范。


三: Tomcat(或其的application container)载入和执行阶段二的来的JAVA二进制码,输出的结果,也就是在客户端见到的,这时隐藏在阶段一和阶段二的参数contentType就发挥了功效


而include指令就是在第一阶段之前执行的,注意这个是在第一阶段之前,所以,如果包含文件和被包含文件的文件编码不一致,那么,该指令就会工作不太正常,不能正确的把被包含的文件从原来编码转换为包含文件的编码,就会出现乱码现象. 

所以这时候得指定html页面的pageEncoding 属性 虽能html文件不识别这个属性  但是最后include到jsp文件中 他会发挥作用的。

没有设置<%@ page language="java"  pageEncoding="UTF-8"%>属性的时候

页面源码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
b.html   中文乱码问题  未加 pageEncoding="UTF-8"%
</body>
</html>

include 到jsp页面后运行的效果  

生成的java文件部分代码


在html页面加入<%@ page language="java"  pageEncoding="UTF-8"%>后 

源码:

<%@ page language="java"  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
b.html   中文乱码问题  未加 pageEncoding="UTF-8"%
</body>
</html>

运行效果 

生成的java文件部分代码


都正常显示。

网上还有一种方法 就是修改项目的web.xml 文件 在web-app标签中加入如下内容

  <jsp-config>
<jsp-property-group>
<description>html encoding example</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.html</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
</jsp-property-group>
</jsp-config>

重新部署下项目就好了

其实仔细观察代码  他也是设置include的html/jsp页面的编码。思路是一样的。



<%@ page contentType=“text/html;charset=GB2312” %> <%@ page import=“java.util." %> <%@ page import="qbeanejbs.”%> <%@ page import=“qbeanclasses.*”%> <%@ page import=“org.apache.log4j.Logger”%> <%@ include file=“…/Include/SpcClientIni.jsp”%> <% System.out.println(“++++++++++++++++++++++++ OCAP Info Entered ++++++++++++++++++++++++++++++”); String sOCAPNo=request.getParameter(“OCAPNo”).trim(); String sChartId=request.getParameter(“ChartId”).trim(); String sDateTime=request.getParameter(“DateTime”).trim(); String sFabName=“”; if (session.getAttribute(“FabName”)==null){ response.sendRedirect(“…/RemindLogin.jsp”); }else { sFabName=session.getAttribute(“FabName”).toString().toUpperCase(); } String sFileURL=“http://”+request.getServerName()+“:”+request.getServerPort()+“/File/” +sFabName+“/”+sDateTime.substring(0,8)+“/”+sOCAPNo+“.xls”; String sFilePath=“\\”+sOCAPFileSrv+“\OCAPSystem\File\”+sFabName+“\”+sDateTime.substring(0,8) + “\”+sOCAPNo+“.xls”; File ocapFile=new File(sFilePath); boolean bFileExist=false; if (ocapFile.exists()){ bFileExist=true; } //System.out.println("* sFileURL= “+sFileURL); //System.out.println(”* sFilePath= “+sFilePath); //System.out.println(”* bFileExist= "+bFileExist); String sDatabaseName=“”; if (session.getAttribute(“DatabaseName”)==null){ response.sendRedirect(“…/RemindLogin.jsp”); }else { sDatabaseName=session.getAttribute(“DatabaseName”).toString(); } /** System.out.println("* OCAPNo= “+sOCAPNo); System.out.println(”* ChartId= “+sChartId); System.out.println(”* OCAPFileURL= “+sOCAPFileURL); System.out.println(”* FileExist= "+sFileExist); **/ Vector OCAPDetailVc=null; Vector OCAPHistVc=null; String sOCAPId=“”; String sReasonCode=“”; String sOCAPHandler=“”; try{ QQuery QQueryBean=((QQueryHome)(EJBHook.getEJB(“QQueryBean”))).create(); // Query OCAP Detail QQueryBean.reset(); /* Modified by Leon at 2005/4/6 for get OCAP Info from new OCAP table String sSQLOCAPDetail=“select * from smic_eocap_detail sed” +" where sed.ocapno=‘" + sOCAPNo + "’“; */ /*String sSQLOCAPDetail=“select * from smic_eocap_transaction” +” where ocapno=‘" + sOCAPNo + "’“; / / Modified by Frank Zhao at 2005/09/12 for sort OCAP history by txntime / String sSQLOCAPDetail="select / MES E007017 created on 2005-09-12 */” +" et.activity,et.action,et.txntime," +" et.username,et.usergroup,et.commentadd,et.status" +" from smic_eocap_transaction et" +" where ocapno=‘" + sOCAPNo + "’ order by et.txntime"; QQueryBean.setQueryInfo(sDatabaseName,sSQLOCAPDetail); OCAPDetailVc=QQueryBean.getResultVector(); // Query OCAP History QQueryBean.reset(); /* Modified by Leon at 2005/4/6 for get OCAP Info from new OCAP table String sSQLOCAPHist="select seh.ocapid,seh.reasoncode,seh.ocaphandle" + " from smic_eocap_history seh where seh.datetime='" + sDateTime + "'" + " and seh.ocapno='" + sOCAPNo + "'"; */ String sSQLOCAPHist="select sei.ocapid,sei.reasoncode,sei.ocaphandle" + " from smic_eocap_info sei where sei.datetime='" + sDateTime + "'" + " and sei.ocapno='" + sOCAPNo + "'"; QQueryBean.setQueryInfo(sDatabaseName,sSQLOCAPHist); OCAPHistVc=QQueryBean.getResultVector(); for (int k=1;k <title>[e-SPC System]-OCAP Detail Info</title> <%@ include file=“…/Include/SpcHead.jsp”%> <form name="frm"> <%if (OCAPDetailVc.size()>1){ for (int i=0;i <%Vector RowOCAPDetailVc=(Vector)OCAPDetailVc.elementAt(i); for (int j=0;j <% //}else if (i!=0 & j!=5){ }else if (i!=0){ %> <%} }%> <% } }else{%> <%}%> <table border="1" cellspacing="1" width="100%" height="30"> <tbody><tr> <td colspan="7"> <b>OCAP No: </b> <%=sOCAPNo%> ,<b> OCAPId:</b> <%=sOCAPId%> <%if (sReasonCode.compareTo("")!=0){%> , <b>ReasonCode:</b> <%=sReasonCode%> <%}%> <%if (sOCAPHandler.compareTo("")!=0){%> , <b>OCAPHandle:</b> <%=sOCAPHandler%> <%}%> </td> </tr><tr><td class="p1"><%=sColumnValue%></td><td class="p11">.<%=sColumnValue%></td></tr> <tr><td colspan="7">No detail history now</td></tr><tr> <td colspan="6" align="right"> <input type="button" name="cmdClose" value="Close"> </td> <td>.</td> </tr> </tbody></table> </form> <% System.out.println(“++++++++++++++++++++++++ OCAP Info Exited ++++++++++++++++++++++++++++++\n”); %> sSQLOCAPDetail 数据了的中文变成了乱码,如何解决?
最新发布
09-03
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值