Jsp中的Application使用
1个回答
展开全部
JSP中Application有两种含义,第一,表示四种属性范围中的“服务器范围”。第二,表示一个内置对象(JSP有九大内置对象)。综合起来也就是说,它是这么一个对象,当服务器打开时它就创建,服务器关闭时,它就消失。
范例:监视器实现,
ServletContextAttribute.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>My JSP 'ServletContextAttribute.jsp' starting page</title>
</head>
<body>
<%
application.setAttribute("name","Hello") ;
application.removeAttribute("name") ;
%>
</body>
</html>
ServletContextListenerDemo.java:
package org.lxh.listener.app;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletContextListenerDemo implements
ServletContextAttributeListener, ServletContextListener {
public void attributeAdded(ServletContextAttributeEvent arg0) {
System.out.println("** 增加属性:" + arg0.getName() + " --> " + arg0.getValue()) ;
}
public void attributeRemoved(ServletContextAttributeEvent arg0) {
System.out.println("** 删除属性:" + arg0.getName() + " --> " + arg0.getValue()) ;
}
public void attributeReplaced(ServletContextAttributeEvent arg0) {
System.out.println("** 替换属性:" + arg0.getName() + " --> " + arg0.getValue()) ;
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("** 容器销毁:" + arg0.getServletContext().getRealPath("/"));
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("** 容器初始化:" + arg0.getServletContext().getRealPath("/"));
}
}
范例:监视器实现,
ServletContextAttribute.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>My JSP 'ServletContextAttribute.jsp' starting page</title>
</head>
<body>
<%
application.setAttribute("name","Hello") ;
application.removeAttribute("name") ;
%>
</body>
</html>
ServletContextListenerDemo.java:
package org.lxh.listener.app;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletContextListenerDemo implements
ServletContextAttributeListener, ServletContextListener {
public void attributeAdded(ServletContextAttributeEvent arg0) {
System.out.println("** 增加属性:" + arg0.getName() + " --> " + arg0.getValue()) ;
}
public void attributeRemoved(ServletContextAttributeEvent arg0) {
System.out.println("** 删除属性:" + arg0.getName() + " --> " + arg0.getValue()) ;
}
public void attributeReplaced(ServletContextAttributeEvent arg0) {
System.out.println("** 替换属性:" + arg0.getName() + " --> " + arg0.getValue()) ;
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("** 容器销毁:" + arg0.getServletContext().getRealPath("/"));
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("** 容器初始化:" + arg0.getServletContext().getRealPath("/"));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询