Hi Legolas, I have actually implemented this -- you first create a small servlet that will auto-load (autoload parameter greater then 0 in your web.xml). This servlet should in it's init() method instantiate a static copy of JSC.
All your other servlets should then be able to pick up the static version of the cache. HOWEVER, in all of this, there is one subtle "gotcha", that has nothing to do with the cache, but in fact, it has something to do with the way static variables are treated in a J2EE application. If you are using an application server that under heavy load will fire off many threads of itself, then, one thread will NOT be able to see a static member in another thread, (websphere will do this on a unix box). However, if you use something like Tomcat or Jboss or WebLogic, then you will probably be ok.. This is probably a discussion for another email list though..... For simple experimentation, I am using the following web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>StartCacheServlet</servlet-name> <servlet-class>test.cache.StartCacheServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>SecondServlet</servlet-name> <servlet-class>test.cache.SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>StartCacheServlet</servlet-name> <url-pattern>/servlet/StartCacheServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SecondServlet</servlet-name> <url-pattern>/servlet/SecondServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> Then, I am using the following StartCacheServlet package test.cache; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.jcs.JCS; public class StartCacheServlet extends HttpServlet { private static JCS jcsCache; /** * Constructor of the object. */ public StartCacheServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { try { System.out.println("cache service starting.."); //String out = (String) treeCache.get("/a","hello"); //System.out.println("out of the cache, I am getting - "+out); // start JCS cache... jcsCache = JCS.getInstance("SalesCentral"); jcsCache.put( "test1", "WOOOOOOO" ); String tq = (String) jcsCache.get("test1"); System.out.println("JCS CACHE VALUE -- "+tq); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } SO, the above is the start-up servlet... Below is a separate servlet that I execute through a browser,... package test.cache; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.jcs.JCS; public class SecondServlet extends HttpServlet { private static JCS jcsCache; /** * Constructor of the object. */ public SecondServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("cache service starting.."); try { jcsCache = JCS.getInstance("SalesCentral"); String tq = (String) jcsCache.get("test1"); System.out.println("JCS CACHE FROM SECONDARY SERVLET -- VALUE -- "+tq); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" The cache content is - "); out.print(tq); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } catch(Exception x) { System.out.println(x); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { // Put your code here } } ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// Again, you can experiment with the above code and web.xml. BEWARE OF THE GOTCHA AS I OUTLINED ABOVE.. Good luck, and tell me how you make out.. Cheers, Aleks. On 5/2/07, Legolas Woodland <[EMAIL PROTECTED]> wrote:
Hi Thank you for reading my post. I need an object cache in my web application and i am looking for some guidelines about implementing it. What i know is: 1- I can use JSC as an in memory cache manager. 2- I need to use a servlet which will initialize when application start, it will should include my cache. Now what i can not understand is: How i can use JCS in a servlet in a way that all my java classes which are inside the web application use it? I read something about singletone and static member objects but i do not know what should i do. Can you please provide some helps? Is it OK to use a servlet as a chache container? Thanks