惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 站得更高,看得更远

Tomcat的class加载的优先顺序一览 汉字转拼音缩写 - 站得更高,看得更远 - 博客园 tomcat 中解决打开xls文件的乱码问题 鼠标在图片上滚动放大或者缩小图片的代码 - 站得更高,看得更远 - 博客园 弹出窗口居中 - 站得更高,看得更远 弹出窗口大全(js) - 站得更高,看得更远 网页常用小技巧(JavaScript) - 站得更高,看得更远 - 博客园 Javascript 小技巧集 LookupDispatchAction, MappingDispatchAction深入分析 Strus 2的新表单标志的使用 Struts 2与AJAX(第二部分) Struts 2与AJAX(第一部分) Struts 2中的OGNL 常用的Struts 2.0的标志(Tag)介绍 在Struts 2.0中国际化(i18n)您的应用程序 在Struts 2.0中实现表单数据校验(Validation) 在Struts 2中实现IoC - 站得更高,看得更远 在Struts 2中实现文件上传 转换器(Converter)——Struts 2.0中的魔术师 - 站得更高,看得更远
使用ServletContextListener在服务器启动和关闭时创建和关闭缓存
站得更高,看得更远 · 2009-03-22 · via 博客园 - 站得更高,看得更远

ServletContext 被 Servlet 程序用来与 Web 容器通信。例如写日志,转发请求。每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享。因为Context可以用来保存资源并且共享,所以我所知道的 ServletContext 的最大应用是Web缓存----把不经常更改的内容读入内存,所以服务器响应请求的时候就不需要进行慢速的磁盘I/O了。

ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。

在JSP文件中,application 是 ServletContext 的实例 ,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。

我们使用缓存的思路大概是:

1 : 服务器启动时,ServletContextListener 的 contextInitialized()方法被调用,所以在里面创建好缓存。可以从文件中或者从数据库中读取取缓存内容生成类,用 servletContext.setAttribute()方法将缓存类保存在 ServletContext 的实例中。

2 : 程序使用 ServletContext.getAttribute()读取缓存。如果是 JSP,使用

application.getAttribute()

如果是 Servlet,使用

getServletContext().getAttribute()

如果缓存发生变化(如访问计数),你可以同时更改缓存和文件/数据库。或者你等 变化积累到一定程序再保存,也可以在下一步保存

3 : 服务器将要关闭时,ServletContextListener 的 contextDestroyed()方法被调用,所以在里面保存缓存的更改。将更改后的缓存保存回文件或者数据库,更新原来的内容。

import  User; 
// my own classimport DatabaseManager; 
//  my own classimport javax.servlet.ServletContext;
import  javax.servlet.ServletContextListener;

 public   class  MyContextListener  implements  ServletContextListener {
    
private  ServletContext context  =   null public   void  contextInitialized(ServletContextEvent event)  {  
        context 
=  event.getServletContext();  
        User user 
=  DatabaseManager.getUserById( 1 );  
        context.setAttribute(
" user1 " , user);
    }
 
  public   void  contextDestroyed(ServletContextEvent event)  {  
        User user 
=  (User)context.getAttribute( " user1 " );  
        DatabaseManager.updateUserData(user);  
        
this .context  =   null
    }

}


布署 ServletContextListener

你实现(implements)了 ServletContextListener 编译后,把它放在正确的WEB-INF/classes目录下,更改WEB-INF目录下的 web.xml文件,在web-app节点里添加

< listener >  
    
< listener - class > MyServletContextListener </ listener - class >
</ listener >