当前位置:网站首页>JSP application对象简介说明
JSP application对象简介说明
2022-07-31 08:23:00 【qq_25073223】
下文笔者讲述JSP中application对象的相关简介说明,如下所示:
application对象: 是一个应用程序对象 当Web服务器启动时 Web服务器会自动创建一个application对象 application对象一旦创建,它将一直存在,直到Web服务器关闭 即:application对象的生命周期为Web服务器启动到Web服务器关闭 ---------------------------------------------------------------- application对象的作用范围: 它是一个应用程序级的对象,它作用于整个Web应用程序, 所有的客户端都共享一个application对象 ------------------------------------------------------------------- 注意事项: application的生命周期比request及session都要长 只要web服务器未关闭或停止,则applicatoin中数据会一直存在
application对象中的方法
| String getAttribute(String name) | 根据属性名称获取属性值 |
| Enumeration getAttributeNames() | 获取所有的属性名称 |
| void setAttribute(String name, Object object) | 设置属性,指定属性名称和属性值 |
| void removeAttribute(String name) | 根据属性名称删除对应的属性 |
| ServletContext getContext(String uripath) | 获取指定URL的ServletContext对象 |
| String getContextPath() | 获取当前Web应用程序的根目录 |
| String getInitParameter(String name) | 根据初始化参数名称,获取初始化参数值 |
| int getMajorVersion() | 获取Servlet API的主版本号 |
| int getMinorVersion() | 获取Servlet API的次版本号 |
| String getMimeType(String file) | 获取指定文件的MIME 类型 |
| String getServletInfo() | 获取当前Web服务器的版本信息 |
| String getServletContextName() | 获取当前Web应用程序的名称 |
| void log(String message) | 将信息写入日志文件中 |
例
获取网站的访问次数
<%@ page import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%! int numbers = 0;%>
<%! public synchronized void count(){
numbers++;
}%>
<%
if(session.isNew()){
count();
String str = String.valueOf(numbers);
session.setAttribute("count",str);
}
application.setAttribute(session.getId(),Integer.toString(numbers));
Enumeration e = application.getAttributeNames();
while(e.hasMoreElements()){
out.println(e.nextElement().toString()+"<br>");
}
%>
<html>
你的sessionID为<%=session.getId()%>
你是第<%=(String)session.getAttribute("count")%>个访问本站的人。
</html>边栏推荐
猜你喜欢

Regarding "computing power", this article is worth reading

Vulkan与OpenGL对比——Vulkan的全新渲染架构
![[MySQL exercises] Chapter 4 · Explore operators in MySQL with kiko](/img/11/66b4908ed8f253d599942f35bde96a.png)
[MySQL exercises] Chapter 4 · Explore operators in MySQL with kiko

深度理解递归,手撕经典递归问题(汉诺塔,青蛙跳台阶),保姆级教学。

SSM框架简单介绍

蚂蚁核心科技产品亮相数字中国建设峰会 持续助力企业数字化转型

Flutter Paystack implements all options
![[MySQL exercises] Chapter 5 · SQL single table query](/img/11/66b4908ed8f253d599942f35bde96a.png)
[MySQL exercises] Chapter 5 · SQL single table query
Doraemon teach you forwarded and redirect page

Pytorch学习记录(七):自定义模型 & Auto-Encoders
随机推荐
First acquaintance with NK-RTU980 development board
云服务器部署 Web 项目
jupyter notebook初使用
Aleo Testnet3规划大纲
MySQL 数据库基础知识(系统化一篇入门)
【Unity】编辑器扩展-04-拓展Scene视图
【MySQL中auto_increment有什么作用?】
高并发高可用高性能的解决方案
状态机动态规划之股票问题总结
一、MySQL主从复制原理
【MySQL功法】第2话 · 数据库与数据表的基本操作
How to Install MySQL on Linux
【小程序专栏】总结uniapp开发小程序的开发规范
Unreal基础概念
Pytorch学习记录(七):自定义模型 & Auto-Encoders
2022 Hangzhou Electric Cup Super League 3
【MySQL功法】第3话 · MySQL中常见的数据类型
基于golang的swagger超贴心、超详细使用指南【有很多坑】
[MySQL exercises] Chapter 3 Common data types in MySQL
刷题《剑指Offer》day06