当前位置:网站首页>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>
边栏推荐
猜你喜欢
【Unity】编辑器扩展-01-拓展Project视图
"C language game" entry-level chess game (robot enhanced version)
Failure scenarios of @Transactional annotations
google搜索技巧——程序员推荐
哆啦a梦教你页面的转发与重定向
免安装版的Mysql安装与配置——详细教程
奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!
会话技术之Coookie && Session详解
ScheduledExecutorService - 定时周期执行任务
[Cloud native and 5G] Microservices support 5G core network
随机推荐
Practical Bioinformatics 2: Multi-omics data integration and mining
MySQL 数据库基础知识(系统化一篇入门)
MySQL 排序
【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符
期刊会议排名、信息检索网站推荐以及IEEE Latex模板下载
New in Visual Studio: Low Priority Builds
SQL 嵌套 N 层太长太难写怎么办?
MySql database optimization query tool
Job hunting product manager [9] How to write a good resume in job hunting season?
科目三:右转弯
jupyter notebook初使用
哪些字符串会被FastJson解析为null呢
Hematemesis summarizes thirteen experiences to help you create more suitable MySQL indexes
"C language" frog jumping steps recursion problem
Flutter Paystack implements all options
Unreal基础概念
刷题《剑指Offer》day06
二维坐标工具API
PowerCLi 通过自建PXE Server一键esxi7下批量部署常规New-VM
【C#】判断字符串中是否包含指定字符或字符串(Contains/IndexOf)