Ecache is used in servlet to report errors, but is normal in simple test cases.

if you want to simply implement a SSO single sign-on system, you can simply simulate it with servlet. Token and other things in the authorization process would like to be placed in

Ecache, then the above problem occurs, and an error is reported directly in service

java.lang.ClassNotFoundException: 

org.ehcache.config.builders.UserManagedCacheBuilder

and no problem in the test.

maven Code

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>3.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>

Ecache simple tool class code

public class EcacheManage {
    private static UserManagedCache<Object, Object> cache;

    static {
        init();
    }

    private static UserManagedCache<Object, Object> getCache() {
        if (cache == null) {
            init();
        }
        return cache;
    }

    private static void init() {
        if (cache != null) {
            return;
        }
       cache = UserManagedCacheBuilder.newUserManagedCacheBuilder(Object.class, Object.class).build(Boolean.TRUE);
    }

    public static Object getObject(Object key) {
        return getCache().get(key);
    }

    public static void putObject(Object key, Object value) {
        getCache().put(key, value);
    }

    public static void emptyEcache(){
        getCache().close();
    }
}

Test case code is normal

public class EcacheTest {
    public static void main(String[] args) {
        EcacheManage.putObject("1", "1");
        String test = (String)EcacheManage.getObject("1");
    }
}

service makes a mistake in servlet

public class SecurityLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user = req.getParameter("user");
        String password = req.getParameter("password");
        String targetURL = req.getParameter("targetURL");
        if (user.equals(password)) {
            HttpSession session = req.getSession();
            String loginToken = UUID.randomUUID().toString().replaceAll("-", "");
            EcacheManage.putObject(loginToken, Boolean.TRUE);//
            session.setAttribute("authenticate", loginToken);
            resp.sendRedirect("authenticate?targetURL=" + targetURL);
        } else {
            resp.sendRedirect("index.jsp?targetURL=" + targetURL);
        }
    }

    @Override
    public void destroy() {
        EcacheManage.emptyEcache();
    }

I don"t know if you have encountered this kind of problem.


tomcat jar package conflict or jar package is lost, Baidu has a lot of answers

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e491ca-44d08.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e491ca-44d08.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?