servlet context cache 综合

来源:互联网 发布:而立不惑知天命的含义 编辑:程序博客网 时间:2024/06/07 01:30

 

 

http://blog.csdn.net/superbeck/article/details/4292969   高性能 servlet  好的文章

 

http://www.open-open.com/lib/view/open1382318768355.html

 

http://www.cnblogs.com/moonlightpoet/p/5617478.html  好的

 

 https://my.oschina.net/fdblog/blog/170022

 

 http://www.open-open.com/lib/view/open1382318768355.html

 

http://www.open-open.com/lib/list/7  综合技术网站 ,比较好的 。

 

 http://www.cnblogs.com/xieyuan/p/3787243.html  缓存配置文档的详细介绍

 https://my.oschina.net/004/blog/214851

 

http://blog.csdn.net/lwx2615/article/details/5624388

http://blog.csdn.net/l271640625/article/details/20528573

http://blog.csdn.net/canot/article/details/51692293

 

 

 

 

public class ContextLoaderListener implements ServletContextListener{    //实现全局上下文初始化方法    @Override    public void contextInitialized(ServletContextEvent servletContextEvent) {        //获得全局变量        ServletContext servletContext = servletContextEvent.getServletContext();        //初始化 EhCache 管理器        CacheManager cacheManager = new CacheManager();        //初始化 defaultCache Cache        Cache cache = cacheManager.getCache("defaultCache");        //新建一个cache节点,并赋值"value1"        Element element = new Element("key1","value1");        //把cache节点存储到默认的cache        cache.put(element);        //将cahe存储到全局变量(servletContext)        servletContext.setAttribute("Cache",cache);    }    //实现全局上下文销毁函数    @Override    public void contextDestroyed(ServletContextEvent servletContextEvent) {        //实现     }}

在 web.xml 里配置 contextInitialized 监听器

<listener>    <listener-class>listener.ContextLoaderListener</listener-class></listener>

3.整合完毕,然后我们就可以测试是否整合成功

因为前面我们初始化的时候已经给cache存储了一个key为“key1”的cache节点
所以我们在helloOut这个servlet中直接查看是否可以取到"key1"节点


public class HelloOut extends HttpServlet {    public void service(HttpServletRequest request,HttpServletResponse response) throws IOException {        PrintWriter writer = response.getWriter();        writer.println("Hello Word For BAE3");        //获取全局变量        ServletContext servletContext = getServletContext();        //从全局变量获取 cache        Cache cache = (Cache) servletContext.getAttribute("Cache");        //输出cache中key为"key1"节点的value        writer.println(cache.get("key1").getObjectValue());        writer.close();    }}

 

 

 

 

 =============================================================================================

 

我自己做的基础实验程序代码:

 

package com;

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.annotation.WebServlet;


/**
 * Servlet implementation class ServletDemo1
 */
@WebServlet("/ServletDemo1")
public class ServletDemo1 extends HttpServlet {
 private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletDemo1() {
        super();
        // TODO Auto-generated constructor stub
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  //response.getWriter().append("Served at: ").append(request.getContextPath());
 
  ServletContext sc = getServletContext();
 // ServletContext sc =   this.getServletContext();
  sc.setAttribute("servletContext", "sc");
 // sc.setAttribute(arg0, arg1);
  response.getWriter().write("OK");
 
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doGet(request, response);
 }

}

--------------------------------

package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletDemo2
 */
@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
 private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletDemo2() {
        super();
        // TODO Auto-generated constructor stub
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 
  //response.getWriter().append("Served at: ").append(request.getContextPath());
 
  
  response.setContentType("text/html");
  response.setCharacterEncoding("GBK");
  PrintWriter out = response.getWriter();
  
  
  ServletContext sc = getServletContext();
  //读取第一个servletContext已经设置好的变量值
  String value = (String)sc.getAttribute("servletContext");
  out.println(value);
  
  //从web。xml中设置一个变量值,并读取此值
  String name=(String) sc.getInitParameter("name");
  out.println(name);
 
  //获取所有参数
  //  Enumeration enumeration=sc.getInitParameterNames();
  //  while (enumeration.hasMoreElements()) {
  //   String name=sc.getInitParameter((String)enumeration.nextElement());
   //  out.println(name+"<br/>");
  //  }
  
 
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doGet(request, response);
 }

}

---------------------------------------------------


 
 

<servlet>
    <servlet-name>ServletDemo1</servlet-name>
    <servlet-class>com.ServletDemo1</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>ServletDemo2</servlet-name>
    <servlet-class>com.ServletDemo2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletDemo1</servlet-name>
    <url-pattern>/servlet/ServletDemo1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ServletDemo2</servlet-name>
    <url-pattern>/servlet/ServletDemo2</url-pattern>
  </servlet-mapping>


<context-param>
   <param-name>name</param-name>
   <param-value>张三</param-value>
  </context-param>

===============================================================

 

 

 oye ,  自己做的首个 ehcache实验代码:

 

package com;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.management.Cache;

/**
 * Application Lifecycle Listener implementation class MyServletContextListener
 *
 */
@WebListener
public class MyServletContextListener implements ServletContextListener
{

 /**
  * Default constructor.
  */
 public MyServletContextListener()
 {
  // TODO Auto-generated constructor stub
 }

 /**
  * @see ServletContextListener#contextDestroyed(ServletContextEvent)
  */
 public void contextDestroyed(ServletContextEvent arg0)
 {
  // TODO Auto-generated method stub
 }

 /**
  * @see ServletContextListener#contextInitialized(ServletContextEvent)
  */
 public void contextInitialized(ServletContextEvent arg0)
 {
  ServletContext sc = arg0.getServletContext();

  // 初始化 EhCache 管理器
  CacheManager manager = CacheManager.newInstance("C:/Users/Administrator/workspace/AppServlet/src/com/ehcache.xml");

 // CacheManager cacheManager = new CacheManager("C:/Users/Administrator/workspace/AppServlet/src/com/ehcache.xml");
  // 初始化 defaultCache Cache
  net.sf.ehcache.Cache cache = manager.getCache("sampleCache1");

 // net.sf.ehcache.Cache cache = cacheManager.getCache("defaultCache");
  // 新建一个cache节点,并赋值"value1"
  Element element = new Element("key2", "value2-magang");
  // 把cache节点存储到默认的cache
  cache.put(element);

  // 将cahe存储到全局变量(servletContext)
  sc.setAttribute("Cache", cache);

  String dogBreed = sc.getInitParameter("breed");
  Dog d = new Dog(dogBreed);
  sc.setAttribute("dog", d);

 }

}

-----------------------------------------------------------------------------

 

package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.management.Cache;

/**
 * Servlet implementation class ListenerTester1
 */
@WebServlet("/ListenerTester1")
public class ListenerTester1 extends HttpServlet {
 private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListenerTester1() {
        super();
        // TODO Auto-generated constructor stub
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
   response.setContentType("text/html");
   PrintWriter writer = response.getWriter(); 
   //获取全局变量  
          ServletContext sc = getServletContext(); 
     //     CacheManager cacheManager = new CacheManager();
          //   net.sf.ehcache.Cache cache = cacheManager.getCache("defaultCache"); 

          //从全局变量获取 cache  
          net.sf.ehcache.Cache   cache = (net.sf.ehcache.Cache)  sc.getAttribute("Cache"); 
          //输出cache中key为"key1"节点的value  
          writer.println(  cache.get("key2").getObjectValue()); 
        
  
  
         writer.println("<br>test context attributes set by listener<br>");
         writer.println("<br>");
         Dog dog = (Dog) getServletContext().getAttribute("dog");
         writer.println("Dog's breed is : " + dog == null ? "no " : dog.getBreed()); 
         writer.close();
 }

 
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doGet(request, response);
 }

}

-----------------------------------------------------------------

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>AppServlet</display-name>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.lyq.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/servlet/MyServlet</url-pattern>
  </servlet-mapping>
 
 
   <servlet>
    <servlet-name>StudentLoginServlet</servlet-name>
    <servlet-class>com.lyq.StudentLoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>StudentLoginServlet</servlet-name>
    <url-pattern>/servlet/StudentLoginServlet</url-pattern>
  </servlet-mapping>
 
 
 
  <servlet>
    <servlet-name>AdminViewNotificationList</servlet-name>
    <servlet-class>com.lyq.AdminViewNotificationList</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AdminViewNotificationList</servlet-name>
    <url-pattern>/servlet/AdminViewNotificationList</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>InsertNotification</servlet-name>
    <servlet-class>com.lyq.InsertNotification</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>InsertNotification</servlet-name>
    <url-pattern>/servlet/InsertNotification</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ViewandCheckJSJDeparmentChongXiu</servlet-name>
    <servlet-class>com.lyq.ViewandCheckJSJDeparmentChongXiu</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ViewandCheckJSJDeparmentChongXiu</servlet-name>
    <url-pattern>/servlet/ViewandCheckJSJDeparmentChongXiu</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ViewandCheckJSJDeparmentChongXiu2</servlet-name>
    <servlet-class>com.lyq.ViewandCheckJSJDeparmentChongXiu2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ViewandCheckJSJDeparmentChongXiu2</servlet-name>
    <url-pattern>/servlet/ViewandCheckJSJDeparmentChongXiu2</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>StudentChongXiuInfofromAPP</servlet-name>
    <servlet-class>com.lyq.StudentChongXiuInfofromAPP</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>StudentChongXiuInfofromAPP</servlet-name>
    <url-pattern>/servlet/StudentChongXiuInfofromAPP</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ViewExamRegistInformationFromApp</servlet-name>
    <servlet-class>com.lyq.ViewExamRegistInformationFromApp</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ViewExamRegistInformationFromApp</servlet-name>
    <url-pattern>/servlet/ViewExamRegistInformationFromApp</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ViewExamRegistInformationFromAdminstation</servlet-name>
    <servlet-class>com.lyq.ViewExamRegistInformationFromAdminstation</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ViewExamRegistInformationFromAdminstation</servlet-name>
    <url-pattern>/servlet/ViewExamRegistInformationFromAdminstation</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ViewExamDoesformationFromApp</servlet-name>
    <servlet-class>com.lyq.ViewExamDoesformationFromApp</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ViewExamDoesformationFromApp</servlet-name>
    <url-pattern>/servlet/ViewExamDoesformationFromApp</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>AcceptStudentChongXiuInfofromApp</servlet-name>
    <servlet-class>com.lyq.AcceptStudentChongXiuInfofromApp</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AcceptStudentChongXiuInfofromApp</servlet-name>
    <url-pattern>/servlet/AcceptStudentChongXiuInfofromApp</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ViewChongXiuBaoMingListConditionfromApp</servlet-name>
    <servlet-class>com.lyq.ViewChongXiuBaoMingListConditionfromApp</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ViewChongXiuBaoMingListConditionfromApp</servlet-name>
    <url-pattern>/servlet/ViewChongXiuBaoMingListConditionfromApp</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>ViewandCheckJSJDeparmentChongXiuAll3</display-name>
    <servlet-name>ViewandCheckJSJDeparmentChongXiuAll3</servlet-name>
    <servlet-class>com.lyq.ViewandCheckJSJDeparmentChongXiuAll3</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ViewandCheckJSJDeparmentChongXiuAll3</servlet-name>
    <url-pattern>/servlet/ViewandCheckJSJDeparmentChongXiuAll3</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>FindServlet</display-name>
    <servlet-name>FindServlet</servlet-name>
    <servlet-class>com.lyq.servlet.FindServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FindServlet</servlet-name>
    <url-pattern>/FindServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>YiJian</display-name>
    <servlet-name>YiJian</servlet-name>
    <servlet-class>com.lyq.servlet.YiJian</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>YiJian</servlet-name>
    <url-pattern>/YiJian</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>YeWuLiuCheng</display-name>
    <servlet-name>YeWuLiuCheng</servlet-name>
    <servlet-class>com.lyq.servlet.YeWuLiuCheng</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>YeWuLiuCheng</servlet-name>
    <url-pattern>/YeWuLiuCheng</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ServletDemo1</servlet-name>
    <servlet-class>com.ServletDemo1</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>ServletDemo2</servlet-name>
    <servlet-class>com.ServletDemo2</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>servlet3</servlet-name>
    <servlet-class>com.servlet3</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>servlet4</servlet-name>
    <servlet-class>com.servlet4</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletDemo1</servlet-name>
    <url-pattern>/servlet/ServletDemo1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ServletDemo2</servlet-name>
    <url-pattern>/servlet/ServletDemo2</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>servlet3</servlet-name>
    <url-pattern>/servlet/servlet3</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>servlet4</servlet-name>
    <url-pattern>/servlet/servlet4</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ListenerTester1</servlet-name>
    <servlet-class>com.ListenerTester1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ListenerTester1</servlet-name>
    <url-pattern>/servlet/ListenerTester1</url-pattern>
  </servlet-mapping>
 
 
  <listener>
    <listener-class>
      com.MyServletContextListener
    </listener-class>
  </listener>
 
   <context-param>
    <param-name>breed</param-name>
    <param-value>Great Dane</param-value>
  </context-param>
  <context-param>
    <param-name>name</param-name>
    <param-value>张三</param-value>
  </context-param>
  <welcome-file-list>
    <welcome-file>testlb.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <distributable/>
</web-app>

-------------------------------------------------

 

ehcache 代码:

 

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

    <diskStore path="java.io.tmpdir"/>

    <defaultCache
            maxEntriesLocalHeap="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskSpoolBufferSizeMB="30"
            maxEntriesLocalDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <cache name="sampleCache1"
           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="1000"
           eternal="false"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
        <persistence strategy="localTempSwap"/>
    </cache>

</ehcache>

==============================================================

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 17个月宝宝0型腿怎么办 走的路多了腿疼怎么办 如果新兵老被老兵打怎么办求求个位 做古董拍卖诈骗被警察抓了怎么办 武汉个人社保怎么办停转到海南来 狗狗车祸前脚软组织受伤了怎么办 6岁儿童夜里盗汗惊战发抖怎么办 一个月大的小狗尾巴掉毛怎么办 口袋妖怪用修改器被ban了怎么办 联币金融提现不到账怎么办 秘密花园的手抄报怎么办?五年级 雷蛇鼠标不亮了怎么办呢 汽车后备箱的垫子好臭怎么办 摩托尾箱内衬味道太重怎么办 买房子把办贷款的单据丢了怎么办 鞋盒破损卖家拒绝退货怎么办 顺丰快递退回发件人签收失败怎么办 客户收到衣服后说衣服破损怎么办 纱窗被老鼠咬了个洞怎么办 双色印刷机开槽刀调不动怎么办 水管软管两端的螺帽下包生锈怎么办 棉花被子盖时间长了被芯变小怎么办 垫的被子发霉了啊没有地方晒怎么办 小孩拉尿在丝棉被棉怎么办 把兔子养大了竟然会咬人怎么办 小兔子生下来母兔子不喂奶怎么办 11个月的宝宝肚子有小白虫怎么办 电焊把脸烤了痒的不行怎么办? 还没满月的宝宝吐奶怎么办 周岁宝宝发烧腹泻吃药老吐怎么办 生完宝宝后肚皮很黑怎么办 两个月宝宝抵抗力差总生病怎么办 两岁宝宝只会说几个简单的词怎么办 两岁宝宝对绘本不感兴趣怎么办 一岁十个月宝宝不爱吃饭怎么办 孩子上三年级学习太差应该怎么办 mac系统打数字出显符号怎么办 我太粗心了工作中总犯错怎么办 四年级的孩子数学计算总粗心怎么办 小孩做作业拖拉的很厉害怎么办 二年级孩子做作业太粗心怎么办