How to configure persistent servlets in Tomcat 6?

Persistent servlets are loaded at server startup and live until the server shuts down.

I have a requirement to run some server-side java programs, and these programs then provide functionality that is completely unique and independent of the web server. I want to do this on a Tomcat startup. The solution I see is a persistent servlet that will invoke these Java programs.

So how to configure persistent servlets in Tomcat 6?

Learn more about persistent servlets from http://java.sun.com/developer/onlineTraining/Servlets/Fundamentals/servlets.html

Temporary and Permanent Servlets

Servlets can be started and stopped for each client request, or they can be started when the web server is up and running until the server is down. Temporary servlets load on demand and offer a good way to store resources on the server for less used functions.

Persistent servlets load at server startup and live until the server stops. Servlets are installed as permanent extensions for the server when their startup costs are very high (for example, establishing a connection to the DBMS), when they offer constant functionality on the server side (for example, the RMI service), or when they should respond to client requests as quickly as possible.

There is no special code needed to make a servlet temporary or permanent; This is a server configuration feature.

Because servlets can load when the web server starts, they can use this automatic loading mechanism to make server-side server program loading easier. These programs can provide functionality that is completely unique and independent of the web server. For example, a servlet can provide R-based services (rlogin, rsh, ...) through TCP / IP ports using the servlet's request / response protocol to represent and process the HTML pages used to control the servlet.

0
source share
2 answers

You can do what you need with ServletContextListener . Register it in web.xml , for example:

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

Then create this 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 } } 
+4
source

I think they mean when they say that “persistent servlets” are just servlets that you define in 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 launched when the server starts and will be stored until it shuts down, possibly (see below).

An example of a temporary servlet would be a .jsp file. It will not download the file until it is requested, and it will probably get rid of it after serving the request.

Looking at the servlet life cycle specification for a servlet,

When the servlet engine starts, the required servlet classes must be located in the servlet container. The servlet container loads the servlet class using the regular Java loading class .... After loading the Servlet class, the container starts it for use.

Then for eol,

A servlet container is not required to store a servlet loaded for any specific time period . A servlet instance can be active in the servlet container for a period in milliseconds, for the life of the servlet container (which can be the number of days, months, or years) or any number of time intervals between them. When the servlet container determines that the servlet should be removed from the service, it calls the destroy method of the Servlet interface to allow the servlet to free any resources it uses and maintain any permanent state. For example, a container can do this when it wants to save memory resources, or when it turns off. Before the servlet container calls the destroy method, it must allow any threads that are currently running in the servlet service method to complete execution or exceed the time limit set by the server. When the destroy method is called on a servlet instance, the container cannot route other requests to this servlet instance. If the container must enable the servlet again, it must do so with a new instance of the servlet class. After the destruction method is complete, the servlet container must empty the servlet so that it has the right to garbage collection.

So, I think that the bottom line, the servlet specification does not give any guarantees as to how long the servlet will be stored, this is a specific implementation, but I think it is a pretty safe bet that if you load it at startup, it will remain loaded for all the time the server is running.

For your specific use case, follow WhiteFang34 as using a servlet for anything other than service requests violates the IMO API.


[Edit] Wherever I look at how the servlet life cycle is managed, it seems that it will remain loaded for the entire lifetime of webapps.

Servlets

What is the difference between JSF, Servlet and JSP?

java servlet instance and session variables

But nowhere can I find a link that says I just don't know for sure.

+1
source

Source: https://habr.com/ru/post/886229/


All Articles