网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> Java编程 >> 文章正文
  jsp中的include乱码问题            【字体:
jsp中的include乱码问题
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-2    
引自:
http://hi.baidu.com/zml525/blog/item/23d99b5243694a0e0cf3e31e.html

解决方法:
1. 问题描述:<%@ include file="*.html" %> 的中文乱码问题

要解决这个问题,当然最简单的就是在每个被 include 的文件第一行,加上

<%@ page contentType="text/html;charset=gb2312" %>
这样一定可以确保中文 jsp 档不会出现乱码,只不过,一旦程序修改成这样的模式,这种程序就无法在旧的
jsp/servlet container 上执行了,因为旧的规格是不允被 include 档案中再出现 <%@ page ... %> 这样的定义的。
正在装载数据……


况且,就算你愿意为了 Tomcat 5.0.x 特别维护一套不同版本的 Source Code,你会遇到重大的挫折,因为 Tomcat 5.0.x 版在 charset 的设定上,会特别检查include 别人的程序与被人include 的程序,这二个程序中所定义的 charset 是不是一样,如果不一样,在编译时就会产生错误。更恐怖的是,竟然还分大小写,比如说:"gb2312" "GB2312" 这样的定义,在 Tomcat 的认定上是不同的。

更好的解决方案
在 Tomcat 5.0.x 中,Tomcat 支持了 JSP 2.0 的规格,同时也支持了部分 J2EE 1.4 的规格,在 J2EE 1.4 的规格中,有关 JSP 的部份,有一个 <jsp-config> 的 XML Tag,这个 XML 区块用来定义与 JSP 相关的特殊属性,包含采用的 taglib 与 以下说明的 <jsp-property-group> ,而解决 include 档中文问题的方法就定义在 <jsp-roperty-group> 中。

在当前应用系统的web.xml里加入jsp-config代码:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<jsp-config>
    <jsp-property-group>  
        <description>     
            Special property group for JSP Configuration JSP example.  
        </description>  
        <display-name>JSPConfiguration</display-name>  
        <url-pattern>*.jsp</url-pattern>  
        <el-ignored>true</el-ignored>  
        <page-encoding>GB2312</page-encoding>  
        <scripting-invalid>false</scripting-invalid>  
        <include-divlude></include-divlude>  
        <include-coda></include-coda>  
       
  <description>     
            Special property group for JSP Configuration JSP example.  
        </description>  
        <display-name>JSPConfiguration</display-name>  
        <url-pattern>*.html</url-pattern>  
        <el-ignored>true</el-ignored>  
        <page-encoding>GB2312</page-encoding>  
        <scripting-invalid>false</scripting-invalid>  
        <include-divlude></include-divlude>  
        <include-coda></include-coda>
    </jsp-property-group>
</jsp-config>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>  

... ...

</webapp>

 

说明:<jsp-config>里的定义,就是通知当前应用服务器,当前应用系统下,所有的 .jsp, .html 文件,若是没有定义contentType="text/html;charset=gb2312" 时,就会采用预设的 "GB2312" 字符集去处理,如此,就不须要在每个 include 的档案第一行加上 contentType="text/html;charset=gb2312" 了。

注:

<jsp-config>标签使用详解

<jsp-config> 包括<taglib> 和<jsp-property-group> 两个子元素。
   
    其中<taglib>元素在JSP 1.2时就已经存在;而<jsp-property-group>是JSP 2.0 新增的元素。
    <jsp-property-group>元素主要有八个子元素,它们分别为:
   
    1.<description>:设定的说明;
    2.<display-name>:设定名称;
    3.<url-pattern>:设定值所影响的范围,如:/CH2 或 /*.jsp;
    4.<el-ignored>:若为true,表示不支持EL 语法;
    5.<scripting-invalid>:若为true,表示不支持<% scripting %>语法;
    6.<page-encoding>:设定JSP 网页的编码;
    7.<include-divlude>:设置JSP 网页的抬头,扩展名为.jspf;
    8.<include-coda>:设置JSP 网页的结尾,扩展名为.jspf。
 
tomcat 5中include页面乱码问题
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<jsp-config>
<jsp-property-group>
<description>Special property group for JSP Configuration JSP example.</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GBK</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-divlude></include-divlude>
<include-coda></include-coda>

<description>Special property group for JSP Configuration JSP example.</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.html</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GBK</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-divlude></include-divlude>
<include-coda></include-coda>
</jsp-property-group>
</jsp-config>

2.关于中文出现乱码的解决办法
 
 
 
在一个编码为utf-8的页面中,使用<jsp:include>包含另一个.jsp/.html文件时,被包含的页面单独浏览正常,但被包含后就会遇到乱码问题。解决的办法是,在每个被包含的页面开始加上下面一行<% page contentType="text/html;charset=utf-8" %>这个方法可以解决jsp include jsp的中文乱码问题。也就是说,被包含的页面必须改成.jsp,哪怕它的内容只有静态html,否则的话还是会出现乱码,如何解决include .html文件中文乱码的问题,还在寻找中。
 
 
 


本文来源:http://blog.csdn.net/szliszt/archive/2007/07/16/1693183.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     用java实现web服务器
     用java快速开发linux gu…
     正则表达式分解siemens交…
     [portal参考手册]目录
     jsp中调用oracle存储过程…
  • JSP中JavaBean的生命周期

  • JSF 的性能远不及 JSP 或 St…

  • JSP学习经验总结

  • Java Swing实现俄罗斯方块

  • Struts2学习:在struts2中集…

  • weblogic 9.1的domain配置my…

  • 保留weblogic 中jsp编译后生…

  • java中final用法

  • 浅析Spring框架下PropertyPl…

  • (JSP)在文本域中显示超链接n…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David