1. include静态指令的使用。
创建名称为ch08的Web项目,编写hello.jsp页面,其中声明一个变量userName,用于获取请求地址后查询串参数userName的值;使用<%@ include>静态指令包含response.jsp页面,通过response.jsp页面显示userName的值,用下面两种方法实现。执行代码并查看运行结果。
方法一:response.jsp页面中通过JSP表达式直接输出变量userName的值。
<%--
Created by IntelliJ IDEA.
User: 28187
Date: 2019/4/27
Time: 16:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>response</title>
</head>
<body>
hello,<%=request.getParameter("username") %><br />
</body>
</html>
方法二:通过pageContext作用域属性,在主页面和子页面间共享userName的值,降低主页面和子页面的依赖性。
<%--
Created by IntelliJ IDEA.
User: 28187
Date: 2019/4/27
Time: 16:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head>
<body>
输入你的名字:<input type="text" id="username" name="username" />
<br />
<input type="submit" value="确定" />
<br /><br />
<%@ include file="response.jsp" %>
</body>
</html>
运行结果:
2. jsp:include动作指令的使用。
编写main.jsp页面,其中声明一个变量userName,用于获取请求地址后查询串参数userName的值;在main.jsp页面中使用jsp:include动作包含subpage.jsp页面,通过subpage.jsp页面显示userName的值;执行代码并查看运行结果。
main.jsp
<%--
Created by IntelliJ IDEA.
User: 28187
Date: 2019/5/6
Time: 21:14
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8"
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>
<jsp:include page="subpage.jsp">
<jsp:param name="userName" value="Tom"/>
</jsp:include>
</body>
</html>
subpage.jsp:
<%--
Created by IntelliJ IDEA.
User: 28187
Date: 2019/5/6
Time: 21:14
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8"