当前位置: 代码迷 >> Java Web开发 >> <jsp:include>和<%@ include %>使用疑惑(结果跟书上讲的不一样)解决方案
  详细解决方案

<jsp:include>和<%@ include %>使用疑惑(结果跟书上讲的不一样)解决方案

热度:933   发布时间:2016-04-17 10:23:15.0
<jsp:include>和<%@ include %>使用疑惑(结果跟书上讲的不一样)
我最近遇到了include使用的问题.让我头痛.主要是感觉跟书籍上说的有出入 
我照着你给的例子试了试发现了得到的结果和您说的不一样(您的说法和书上的一样) 
<jsp:include>的用法和您的一致,问题就出在使用<%@ include file=""%>标签的时候. 

//======date.jsp的源文件=====//
<%@ page contentType="text/html;charset=gb2312"%>
<%
java.util.Date date=new java.util.Date();
String date_cn ="";
String dateStr = "";
switch(date.getDay())
{
case 0:date_cn ="日"; break;
case 1:date_cn ="一"; break;
case 2:date_cn ="二"; break;
case 3:date_cn ="三"; break;
case 4:date_cn ="四"; break;
case 5:date_cn ="五"; break;
case 6:date_cn ="六"; break;
}
dateStr = (1900+date.getYear()) + "年" + (date.getMonth()+1) + "月" + date.getDate() + "日(星期" + date_cn + ")";


%>
<%=dateStr%>
<%
out.print("I am here ,in date.jsp");
%>


//======以下是test.jsp的源文件=============//
<%@ page contentType=”text/html;charset=gb2312”%>
<html>
<head>
<title>include的两种用法</title>
<%--<jsp:include page="date.jsp" flush="true"/>--%>
<%@ include file="date.jsp"%>
<head>
<body>
<table><tr><td>
有关jsp中include的两种用法.敬请关注。
</td></tr></table>
</body>
</html>

当我不在date.jsp中添加<%@ page language=”java” contentType="text/html;charset=gb2312"%>在test.jsp中使用<%@ include file="date.jsp"%> 
时.得到的时间为乱码.
 
而当我在date.jsp中添加<%@ page language=”java” contentType="text/html;charset=gb2312"%>在test.jsp中使用<%@ include file="date.jsp"%> 
时却能够得到正确显示的时间,并没有得到预期的错误提示. 
HTTP Status 500 ?
org.apache.jasper.JasperException: /date.jsp(0,0) Page directive: can't have multiple occurrences of contentType

我在不同的电脑上试过,在不同的浏览器上试过,都是这样的.不知道问题究竟出在哪,请大家帮忙指点一下

编译后在临时文件中找到了两个文件,一个test_jsp.java和一个test_jsp.class文件
打开test_jsp.java文件后文件内容如下
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
  implements org.apache.jasper.runtime.JspSourceDependent {

  private static java.util.List _jspx_dependants;

  static {
  _jspx_dependants = new java.util.ArrayList(1);
  _jspx_dependants.add("/date.jsp");
  }

  public Object getDependants() {
  return _jspx_dependants;
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
  throws java.io.IOException, ServletException {

  JspFactory _jspxFactory = null;
  PageContext pageContext = null;
  HttpSession session = null;
  ServletContext application = null;
  ServletConfig config = null;
  JspWriter out = null;
  Object page = this;
  JspWriter _jspx_out = null;
  PageContext _jspx_page_context = null;
  try {
  _jspxFactory = JspFactory.getDefaultFactory();
  response.setContentType("text/html; charset=GB2312");
  pageContext = _jspxFactory.getPageContext(this, request, response,
  null, true, 8192, true);
  _jspx_page_context = pageContext;
  application = pageContext.getServletContext();
  config = pageContext.getServletConfig();
  session = pageContext.getSession();
  out = pageContext.getOut();
  _jspx_out = out;
  相关解决方案