0

Permanent servlets are loaded when the server is started, and live until the server is shutdown.

I have requirement to start some server side java programs and These programs then provide functionality that is totally unique and independent of the web server. I wish to do it on Tomcat startup. Solution I see is Permanent Servlet, Which will invokes these Java programs.

So How to configure Permanent Servlets in Tomcat 6 ?

More Details about Permanent Servlets from http://java.sun.com/developer/onlineTraining/Servlets/Fundamentals/servlets.html

Temporary versus Permanent Servlets

Servlets can be started and stopped for each client request, or they can be started as the web server is started and kept alive until the server is shut down. Temporary servlets are loaded on demand and offer a good way to conserve resources in the server for less-used functions.

Permanent servlets are loaded when the server is started, and live until the server is shutdown. Servlets are installed as permanent extensions to a server when their start-up costs are very high (such as establishing a connection with a DBMS), when they offer permanent server-side functionality (such as an RMI service), or when they must respond as fast as possible to client requests.

There is no special code necessary to make a servlet temporary or permanent; this is a function of the server configuration.

Because servlets can be loaded when a web server starts, they can use this auto-loading mechanism to provide easier loading of server-side Java programs. These programs can then provide functionality that is totally unique and independent of the web server. For example, a servlet could provide R-based services (rlogin, rsh, ...) through TCP/IP ports while using the servlet request/response protocol to present and process HTML pages used to manage the servlet.

Community
  • 1
  • 1
YoK
  • 14,329
  • 4
  • 49
  • 67

2 Answers2

4

You can do what you need with a ServletContextListener. Register one in your web.xml, e.g.:

<listener>
    <listener-class>com.example.MyServletContextListener</listener-class>
</listener>

Then create that class to do what you want:

public class MyServletContextListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        // initialize and startup whatever you need here
    }

    public void contextDestroyed(ServletContextEvent sce) {
        // shutdown and destroy those things here
    }
}
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
  • This looks like good alternative to Permanent Servlet. But my question is how to configure Permanent Servlet ? – YoK Apr 14 '11 at 06:43
  • 1
    Fair enough, good question. Everything I've seen says that a permanent servlet is a matter of configuration, but no where have I ever seen how to configure one. The closest I'm aware of is the `` element in `web.xml`. I'd still lean toward a `ServletContextListener` though, since it's designed for what you need. – WhiteFang34 Apr 14 '11 at 07:07
  • I have the feeling ( but no proof unfortunately ) that the default would be for servlets to be permanent. But, out of interest, if you need to start these servlets to provide "functionality that is totally unique and independent of the web server" it sounds like they do not rely on the request-response paradigm. In which case, why not start one or more threads from the context listener - there's no to implement the functionality as servlets. – DaveH Apr 14 '11 at 07:42
  • Accepting this answer as its good alternative and my requirements match. But If some actual Answer regarding Permanent Servlet configuration is provided, I would have to accept that answer :). – YoK Apr 15 '11 at 05:28
1

I think what they are referring to when they say "permanent servlets" are just the servlets you define in the web.xml, a la,

<servlet>
  <description>I'm permanent</description>
  <display-name>Servlet</display-name>
  <servlet-name>Servlet</servlet-name>
  <servlet-class>com.servlet.MyServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

This servlet will be started up when the server starts and will be kept around until it shuts down, probably (See below).

An example temporary servlet would be a .jsp file. It wont load the file up until it is requested and it will probably dispose of it after servicing the request.

Looking at the Servlet Specification about servlet lifecycles,

When the servlet engine is started, needed servlet classes must be located by the servlet container. The servlet container loads the servlet class using normal Java class loading facilities. ... After loading the Servlet class, the container instantiates it for use.

Then for eol,

The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between. When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down. Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit. Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. If the container needs to enable the servlet again, it must do so with a new instance of the servlet’s class. After the destroy method completes, the servlet container must release the servlet instance so that it is eligible for garbage collection.

So I think bottom line, the servlet specification makes no guarantee as to how long a servlet will be kept, it is implementation specific, but I think its a pretty safe bet that if you load it on start up it will remain loaded for the whole time the server is running.

For your specific use case though, follow WhiteFang34, as using a servlet for something other than servicing requests is abusing the API IMO.


[Edit] Everywhere I look to see how a servlets' lifecycle is managed seems to say that it will remain loaded for the entirety of the webapps lifetime.

Servlets

What is the difference between JSF, Servlet and JSP?

java servlet instantiation and session variables

But nowhere can I find a reference that says that, so I just don't know for certain.

Community
  • 1
  • 1
Andrew
  • 13,757
  • 13
  • 66
  • 84
  • As per your last comment, read the Servlet API specs of all servlet versions from 1.0 until 3.0. Also please note that the article the poor OP has linked in his question is written at 1998. – BalusC Apr 19 '11 at 23:44
  • @BalusC I'm just overthinking this stuff, I read the parts I think are relevant and I can't find anything that says the servlet will be kept for the lifetime of the container, just that it CAN be kept for that long. Can you show me where it outlines that? – Andrew Apr 20 '11 at 14:35