---
title: "JSP的include指令"
author: "cakin24"
date: "2017-10-08T08:40:29Z"
tags: ["JSP", "include"]
categories: ["编程语言"]
slug: "jsp-include"
draft: false
lastmod: "2017-10-08T08:40:29Z"
id: 2395465
url: "https://cakin24.iteye.com/blog/2395465"
---

一 语法

&lt;%@ include file="URL"%&gt;

 

二 代码

1、date.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8"%&gt;

&lt;%@ page import="java.text.\*" %&gt;

&lt;%

   Date d = new Date();

   SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

   String s = sdf.format(d);

   out.println(s);

%&gt;

2、include\_command.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'Include\_Command.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;h1&gt;Include指令&lt;/h1&gt;

    &lt;hr&gt;

    &lt;%@ include file="date.jsp"%&gt;

  &lt;/body&gt;

&lt;/html&gt;

 

三 测试结果

 

![](http://dl2.iteye.com/upload/attachment/0127/2650/2330f40c-6daf-399a-8f65-d1d6c26d99d9.png)