08_jsp

一.静态包含

  • 格式 : <%@include file=“” %> , file表示包含的路径 , 通常以 “/” 开头,表示 http://ip:port/工程路径
  • 特点: 1. 不会翻译被包含的jsp页面 2.其实是将被包含的jsp页面代码拷贝到对应位置输出
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>测试静态包含</h1>

    主体信息 <br>

    <%@include file="/footer/footer.jsp"%>

</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    底部信息<br/>

</body>

</html>

测试结果[找到翻译的java文件]

在这里插入图片描述

二.动态包含

  • 格式: <jsp:include page=“”></jsp:include> , 其中page为包含的jsp页面, 通常以 “/” 开头,表示 http://ip:port/工程路径
  • 特点: 1.会将包含的jsp页一起翻译为java代码 2.底层代码调用被包含的jsp页面执行输出 3.动态包含还可以传递参数
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>测试动态包含</h1>
    主体信息 <br>
    <jsp:include page="/footer/footer.jsp">
        <jsp:param name="username" value="Tony"/>
    </jsp:include>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    底部信息<br/>
    获取username: <%=request.getParameter("username")%>
</body>

</html>

测试结果

在这里插入图片描述


org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "/footer/footer.jsp" + "?" + org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode("username", request.getCharacterEncoding())+ "=" + org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode("Tony", request.getCharacterEncoding()), out, false);

包含的路径,以及参数的传递 http://localhost:8080/web03/footer/footer.jsp?username=Tony

三.请求转发

  • 格式: <jsp:forward page=“”></jsp:forward>
  • 功能: 请求转发

四.Listener监听器

Listener是JavaEE规范.就是接口.监听某种事物的变化,然后通过回调函数反馈给程序做相应的处理

ServletContextListener

监听servletContext对象的创建和销毁.

在这里插入图片描述

使用流程

  • 创建一个类,实现servletContextListener的两个方法
package com.example.web03.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("监听 - context对象创建");
    }


    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("监听 - context对象销毁");
    }
}
  • 在web.xml文件中对监听器进行配置
<!--配置自定义监听器-->
<listener>
	<listener-class>com.example.web03.listener.MyListener</listener-class>
</listener>

在配置后,如果项目启动,需要重新部署才可以生效
监听器进行配置

<!--配置自定义监听器-->
<listener>
	<listener-class>com.example.web03.listener.MyListener</listener-class>
</listener>

在配置后,如果项目启动,需要重新部署才可以生效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值