Web组件重用与JavaBeans

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"
     
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值