Example usage for javax.servlet Servlet init

List of usage examples for javax.servlet Servlet init

Introduction

In this page you can find the example usage for javax.servlet Servlet init.

Prototype


public void init(ServletConfig config) throws ServletException;

Source Link

Document

Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

Usage

From source file:org.ops4j.pax.web.service.internal.FilterTest.java

@Test
public void filterIsCalledOnUrlPattern() throws NamespaceException, ServletException, IOException {
    Servlet servlet = createMock(Servlet.class);
    servlet.init((ServletConfig) notNull());
    servlet.destroy();//from  w  w w  . j ava  2s. c om

    Filter filter = createMock(Filter.class);
    filter.init((FilterConfig) notNull());
    filter.doFilter((ServletRequest) notNull(), (ServletResponse) notNull(), (FilterChain) notNull());
    filter.destroy();

    replay(servlet, filter);

    HttpContext context = m_httpService.createDefaultHttpContext();
    m_httpService.registerServlet("/test", servlet, null, context);
    m_httpService.registerFilter(filter, new String[] { "/*" }, null, context);

    HttpMethod method = new GetMethod("http://localhost:8080/test");
    m_client.executeMethod(method);
    method.releaseConnection();

    m_httpService.unregister("/test");
    m_httpService.unregisterFilter(filter);

    verify(servlet, filter);
}

From source file:org.ops4j.pax.web.service.internal.FilterTest.java

@Test
public void filterIsCalledOnServlet() throws NamespaceException, ServletException, IOException {
    Servlet servlet = createMock(Servlet.class);
    servlet.init((ServletConfig) notNull());
    servlet.destroy();// ww  w  . jav a2 s  .  c  o m

    Filter filter = createMock(Filter.class);
    filter.init((FilterConfig) notNull());
    filter.doFilter((ServletRequest) notNull(), (ServletResponse) notNull(), (FilterChain) notNull());
    filter.destroy();

    replay(servlet, filter);

    HttpContext context = m_httpService.createDefaultHttpContext();
    m_httpService.registerServlet("/test", servlet, null, context);
    m_httpService.registerFilter(filter, null, new String[] { "/test" }, context);

    HttpMethod method = new GetMethod("http://localhost:8080/test");
    m_client.executeMethod(method);
    method.releaseConnection();

    m_httpService.unregister("/test");
    m_httpService.unregisterFilter(filter);

    verify(servlet, filter);
}

From source file:org.pentaho.platform.web.servlet.PluginDispatchServlet.java

@SuppressWarnings("unchecked")
/** Restore the caching once the Plugin Type Tracking system is in place, for now we'll look-up every time **/
private synchronized void doInit() throws ServletException {
    if (logger.isDebugEnabled()) {
        logger.debug("PluginDispatchServlet.init"); //$NON-NLS-1$
    }/* w w  w.  ja v  a 2  s  .c o  m*/

    if (initialized) {
        return;
    }
    pluginServletMap.clear();

    Map<String, ListableBeanFactory> pluginBeanFactoryMap = getPluginBeanFactories();

    for (Map.Entry<String, ListableBeanFactory> pluginBeanFactoryEntry : pluginBeanFactoryMap.entrySet()) {

        Map<String, Object> beans = BeanFactoryUtils
                .beansOfTypeIncludingAncestors(pluginBeanFactoryEntry.getValue(), Servlet.class, true, true);

        if (logger.isDebugEnabled()) {
            logger.debug("found " + beans.size() + " servlets in " + pluginBeanFactoryEntry.getKey()); //$NON-NLS-1$//$NON-NLS-2$
        }

        for (Map.Entry<String, Object> beanEntry : beans.entrySet()) {
            Servlet pluginServlet = (Servlet) beanEntry.getValue();
            String servletId = beanEntry.getKey();

            String pluginId = pluginBeanFactoryEntry.getKey();
            String context = pluginId + "/" + servletId; //$NON-NLS-1$

            pluginServletMap.put(context, pluginServlet);
            if (logger.isDebugEnabled()) {
                logger.debug("calling init on servlet " + pluginServlet.getClass().getName()
                        + " serving context " + context); //$NON-NLS-1$//$NON-NLS-2$
            }
            try {
                pluginServlet.init(servletConfig);
            } catch (Throwable t) {
                logger.error("Could not load servlet '" + context + "'", t); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            }
        }
    }

    // Set initialized to true at the end of the synchronized method, so
    // that invocations of service() before this method has completed will not
    // cause NullPointerException
    initialized = true;
}