-1

I have web application in Java code, which uses servlets. My question is how to initialize some java class, which is not servlet. I understand that if the client connects - the servlet then prints the output. But only "if client connects".

Is it possible to run some threads before any connections are made?

EDIT:

Thanks to answers, right now I'm trying to do it this way:

the class:

package com.xsistema.filemanager.application;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 * @author Ernestas Gruodis
 */
public class ServerInit implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Initialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Destroyed");
    }
}

And the glassfish-web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
    <context-root>/file-manager</context-root>
    <class-loader delegate="true"/>
    <jsp-config>
        <property name="keepgenerated" value="true">
            <description>Keep a copy of the generated servlet class' java code.</description>
        </property>
    </jsp-config>

    <listener>
        <listener-class>
            com.xsistema.filemanager.application.ServerInit
        </listener-class>
    </listener>

</glassfish-web-app>

And I getting this error while deploying the application:

Warning: Unsupported deployment descriptors element listener-class value com.xsistema.filemanager.application.ServerInit.

What's wrong here?

EDIT2:

Can not delete this question, appeared to be duplicate (it has the answers already). But I found the solution:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.servlet.ServletContextEvent;

@Startup
@Singleton
public class Config {

    @PostConstruct
    public void init() {
        // Do stuff during webapp's startup.
    }

    @PreDestroy
    public void destroy() {
        // Do stuff during webapp's shutdown.
    }
}

Very nice and easy, and working :)

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117

2 Answers2

3

The preferred way to do this is to install a ServletContextListener.

These get a chance to run code when the container starts up and shuts down.

If the code affects only a single servlet (or needs to initialize something private to that servlet object), you could also do the work in the init method of the servlet and make sure that the servlet is loaded even before a request is made (via the load-on-startup parameter).

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

You may run your code that initializes the other classes in the init() method of one of your servlets.

david a.
  • 5,283
  • 22
  • 24
  • But that means I have to add the same code on all servlets, or do some filter.. Not good. – Ernestas Gruodis Oct 07 '15 at 08:29
  • if it's something you need to perform before any of you servlets is called, then yes. In that case, I'd let all servlets extend one base servlet class with such a logic. – david a. Oct 07 '15 at 08:41
  • Ok, ServletContextListener is more appropriate here, if it's an application-wide initialization. I'm upvoting the other answer :) – david a. Oct 07 '15 at 08:47
  • " I have to add the same code on all servlets,". Well, not really, depends on what that code does. While ServletContextListener is probably the cleaner way, it is functionally almost equivalent to putting the same code into a single Servlet and loading that on startup. That's what people did before there were ServletContextListeners. The effect (how it impacts this and other servlets) is the same either way. – Thilo Oct 07 '15 at 09:13