Servlet学习(七)Application
来源:互联网 发布:gta5ol女角色捏脸数据 编辑:程序博客网 时间:2024/06/02 13:12
Session是服务器内,针对某个特定客户端(浏览器)的特定内存区域。
而Application是在webapp生命周期内,提供给所有客户端的服务器内的内存区域。
Application在API中对应类ServletContext。
context:上下文,一个context相当一个webapp的运行环境。两者一一对应。
servletContext通过HttpServlet的getServletContext()方法就可以拿到,
通过ServletContext的getAttribute()和setAttribute()方法获取/设置相关属性。
测试程序:
package com.vin.application;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Application测试 * 用于保存整个web应用的生命周期内都可以访问的数据 * 可供多个不同窗口访问,可作为某一页面被总共访问次数的计数器(比如网站首页的访问量) * @author Pigeon * */public class TestServletContext extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); // Returns a reference to the ServletContext in which this servlet is running. ServletContext application = this.getServletContext(); // 从当前application中读取属性accessCount的值 Integer accessCount = (Integer)application.getAttribute("accessCount"); if(accessCount == null) { accessCount = new Integer(0); } else { accessCount = new Integer(accessCount.intValue() + 1); } // 向当前application中插入键(key,属性)值(value)对 application.setAttribute("accessCount", accessCount); out.println("<html><head><title>ServletContext测试</title></head><br>" + "<body><td>" + accessCount +"</td>/n" + "</body></html>"); }}结果:每次刷新,访问次数+1.
与session不同的是,session刷新页面,访问次数+1。但重新打开页面,访问次数归零。(关闭页面即关闭旧session,打开新session)
application刷新页面,访问次数+1;重新打开页面,访问次数不归零。(application的context上下文存货于整个webapp的生命周期)
0 0
- Servlet学习(七)Application
- servlet学习(七)
- Servlet学习笔记(七):Session详解
- servlet学习总结七
- Java Servlet学习笔记(七)Servlet Session跟踪
- servlet (七)javaBean
- Android 学习笔记之七 Application类
- servlet初步(六) application
- JSP&&SERVLET学习笔记(七):Servlet处理上传的文件
- servlet Application
- javaee学习之路(七)Servlet监听器
- Servlet学习之七MV模型
- Flex企业应用开发实践学习笔记(七)——Flex Application
- Java for Web学习笔记(七):Servlet(5)初始化参数
- GeoServer学习手记(七):Servlet及HTTP派发过程之四
- GeoServer学习手记(七):Servlet及HTTP派发过程之四
- Servlet学习笔记(七)—— 自定义过滤器的编写改进:自定义实现FilterChain
- java学习之Servlet之--Cookie,Session,Application
- The difference among VIRT, RES, and SHR in top output
- C语言KMP字符串匹配算法
- Twig 模板的Filters全纪录
- ijkplayer编译(Ubuntu + Win双系统)
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸
- Servlet学习(七)Application
- Spring 注解的方式进行事务管理
- log(二)——MDC实现之ThreadLocal
- PAT1004 Counting Leaves
- C语言系统资源控制(getrlimit && setrlimit)
- windowsViaC/C++之设备I/O之文件设备
- firebase基础(一)<推送服务调研>
- 在新linux中使用proc_create_data编写proc
- 兔子与樱花(map+最短路+记录路径)