javaWeb之监听器的分类2

时间:2022-04-19 13:46:33

javaWeb之监听器的分类2

javaWeb之监听器的分类2
代码:
MyServletContextAttributeListener.java:

package com.mooc.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class MyServletContextAttributeListener implements
ServletContextAttributeListener {


@Override
public void attributeAdded(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
System.out.println("ServletContext_attributeAdded:"+arg0.getName());

}

@Override
public void attributeRemoved(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
System.out.println("ServletContext_attributeRemoved:"+arg0.getName());

}

@Override
public void attributeReplaced(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
System.out.println("ServletContext_attributeReplaced:"+arg0.getName());

}

}

MyHttpSessionAttributeListener.java:

package com.mooc.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class MyHttpSessionAttributeListener implements
HttpSessionAttributeListener {


@Override
public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("HttpSession_attributeAdded:"+arg0.getName());

}

@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("HttpSession_attributeRemoved:"+arg0.getName());

}

@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("HttpSession_attributeReplaced:"+arg0.getName());

}

}

MyServletRequestAttributeListener.java:

package com.mooc.listener;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;

public class MyServletRequestAttributeListener implements
ServletRequestAttributeListener {


@Override
public void attributeAdded(ServletRequestAttributeEvent arg0) {
System.out.println("ServletRequest_attributeAdded:"+arg0.getName());

}

@Override
public void attributeRemoved(ServletRequestAttributeEvent arg0) {
System.out.println("ServletRequest_attributeRemoved:"+arg0.getName());

}

@Override
public void attributeReplaced(ServletRequestAttributeEvent arg0) {
System.out.println("ServletRequest_attributeRemoved:"+arg0.getName());

}

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FirstListener</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.mooc.listener.FirstListener</listener-class>
</listener>
<context-param>
<param-name>initParam</param-name>
<param-value>mooc</param-value>
</context-param>
<listener>
<listener-class>com.mooc.listener.MyHttpSessionListener</listener-class>
</listener>
<session-config>
<session-timeout>1</session-timeout><!-- 一分钟后session被销毁 -->
</session-config>
<listener>
<listener-class>com.mooc.listener.MyServletRequestListener</listener-class>
</listener>
<listener>
<listener-class>com.mooc.listener.MyHttpSessionAttributeListener</listener-class>
</listener>
<listener>
<listener-class>com.mooc.listener.MyServletContextAttributeListener</listener-class>
</listener>
<listener>
<listener-class>com.mooc.listener.MyServletRequestAttributeListener</listener-class>
</listener>
</web-app>

index.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
This is my JSP page. <br>
<button onclick="location.href='<%=request.getContextPath()%>/init.jsp';">Init</button><br>
<button onclick="location.href='<%=request.getContextPath()%>/destory.jsp';">Destory</button>
</body>
</html>

运行index.jsp的结果:

requestInitialized name=null
ServletRequest_attributeRemoved:org.apache.catalina.ASYNC_SUPPORTED
ServletContext_attributeAdded:org.apache.jasper.runtime.JspApplicationContextImpl
sessionCreated
requestDestroyed

init.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

request.setAttribute("requestName","requestValue");
request.getSession().setAttribute("sessionName", "sessionvalue");
request.getSession().getServletContext().setAttribute("contextName", "contextValue");
// request.getAttribute(s) 这个值放在request预对象里面的一个属性,只有先setAttribute()之后才能使用getAttribute()
// request.getParameter(s) request请求的一个参数 http://www.baidu.com/username=zhangsan
// request.setAttribute("requestName","requestValue");输出ServletRequest_attributeReplaced:requestName

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'init.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


</head>

<body>
<h1>这是初始化值的界面</h1>
</body>
</html>

init.jsp运行结果:


第一遍运行:
requestInitialized name=null
ServletRequest_attributeRemoved:org.apache.catalina.ASYNC_SUPPORTED
ServletRequest_attributeAdded:requestName
HttpSession_attributeAdded:sessionName
ServletContext_attributeAdded:contextName
requestDestroyed
第二遍运行:
requestInitialized name=null
ServletRequest_attributeRemoved:org.apache.catalina.ASYNC_SUPPORTED
ServletRequest_attributeAdded:requestName
HttpSession_attributeReplaced:sessionName
ServletContext_attributeReplaced:contextName
requestDestroyed
要想输出ServletRequest_attributeReplaced:requestName,在后面再写一遍request.setAttribute("requestName","requestValue");

destory.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

request.removeAttribute("requestName");
request.getSession().removeAttribute("sessionName");
request.getSession().getServletContext().removeAttribute("contextName");
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'destory.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


</head>

<body>
<h1>这是销毁界面</h1>
</body>
</html>

destory.jsp运行结果:

requestInitialized name=null
ServletRequest_attributeRemoved:org.apache.catalina.ASYNC_SUPPORTED
HttpSession_attributeRemoved:sessionName
ServletContext_attributeRemoved:contextName
requestDestroyed