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>
==============================================================