我正在使用 Guava 14.0 jar,以下是我的代码
package org.user.module.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.Cache;
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 test
*/
@WebServlet("/test")
public class test extends HttpServlet {
private static final long serialVersionUID = 1L;
Cache<String, Integer> map;
public test() {
super();
// TODO Auto-generated constructor stub
}
public void init() throws ServletException
{
map=CacheBuilder.newBuilder().
maximumSize(100).
expireAfterAccess(10, TimeUnit.MINUTES).
build();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
String p=request.getParameter("v");
Integer a=map.getIfPresent(p);
if(a==null){
map.put(p, 1);
out.println("Putting The Integer:"+a);
}
else{
map.put(p,a++);
out.println("A is already present and the value is:"+a);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
错误:
严重:为 servlet org.user.module.test.test java.lang.ClassNotFoundException 分配异常:com.google.common.cache.Cache at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546) 在 java.lang.Class.getDeclaredFields0(Native Method) 在 java.lang.Class.privateGetDeclaredFields(Class.java:2317) 在 java.lang。 Class.getDeclaredFields(Class.java:1762) at org.apache.catalina.core.DefaultInstanceManager.populateAnnotationsCache(DefaultInstanceManager.java:276) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:143) at org .apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:125) 在 org.apache.catalina.core。StandardWrapper.loadServlet(StandardWrapper.java:1136) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:857) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) at org .apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve .java:168)在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 在 org.apache.catalina。 core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) 在 org.apache.tomcat.util.net 的 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987) .JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:722)runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 在 java.lang.Thread.run(Thread.java:722)runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 在 java.lang.Thread.run(Thread.java:722)