当前位置:网站首页>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>边栏推荐
猜你喜欢
随机推荐
傅里叶变换,拉普拉斯变换学习记录
[MySQL exercises] Chapter 2 Basic operations of databases and data tables
SSM框架讲解(史上最详细的文章)
7/28-7/29 Expectation + thinking + suffix array + ST table
Cloud server deployment web project
SQL statement knowledge
How on one machine (Windows) to install two MYSQL database
【pytorch记录】pytorch的分布式 torch.distributed.launch 命令在做什么呢
Flutter Paystack 所有选项实现
普通函数的参数校验
循环结构--for循环
sqli-labs(less-11)
The torch distributed training
《如何戒掉坏习惯》读书笔记
免安装版的Mysql安装与配置——详细教程
模块化规范
【MySQL功法】第5话 · SQL单表查询
MySQL 数据库基础知识(系统化一篇入门)
服务器上解压文件时提示“gzip: stdin: not in gzip format,tar: Child returned status 1,tar: Error is not recovera“
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Part 1)--Component UI









