Java servlet pool

Servlets 101 under Tomcat 6:

Someone can give me a good explanation of the best way, for example Create a collection of expensive foo objects at servlet startup and store them where I can access them when processing each request?

I can say there are at least three ways to do this, and I'm a little vague about the difference I don't care about clustering or algorithms to drive out old items or similar things, just the basics

Cheers, thank you

Solution

Implement servletcontextlistener, perform the required loading tasks during contextinitialized(), and store the results in the application scope through servletcontext#setattribute() It will be called during server startup, and the application scope can also be accessed in a regular servlet

Basic example:

public class Config implements servletcontextlistener {
   public void contextInitialized(ServletContextEvent event) {
        List<Foo> foos = fooDAO().list();
        event.getServletContext().setAttribute("foos",foos);
    }
}

The usual way is on the web Map it in XML:

<listener>
    <listener-class>mypackage.Config</listener-class>
</listener>

The following is how to access it in a regular servlet:

protected void doSomething(request,response) {
    List<Foo> foos = (List<Foo>) getServletContext().getAttribute("foos");
}

Here is how to access it in jsp:

<c:forEach items="${foos}" var="foo">
    ${foo.someProperty}<br>
</c:forEach>

In other words, I really don't know what this has to do with the "servlet pool" The word is meaningless

I hope this will help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>