Table of Contents
In this tutorial, we will identify some of the possible causes that a servlet context listener can trigger, and then provide possible fixes that you can try to resolve the issue.
PC running slow?
g.The ServletContextListener receives notifications of changes to the servlet context and commands an action. ServletContextListener is used to do important work when the situation is initialized and destroyed.
g.
Size = “- 1″>Javax.servlet
ServletContextListener
What is a servlet listener used for?
Servlet Listener is used to listen when you want to listen for events in a web container, for example so you can set up listeners on the internet. xml, for some reason HttpSessionListener.
- All super-interfaces:
- java.util.EventListener
-
public ServletContextListener
- extends java.util.EventListener
To receive these notification events, some class implementations must be declared in the web deployment descriptor -Method, annotated with WebListener
, or registered with only one of the addListener methods ServletContext
.
Implementations of this interface are called from you The contextInitialized (javax.servlet.ServletContextEvent) method
in the order they were used. declared and the contextDestroyed (javax.servlet.ServletContextEvent)
method is inverted Mission.
- C:
- Servlet 2.3
- See also:
-
ServletContextEvent
ContextInitialized
- Recipients notice that initializing the web application work begins.
All ServletContextListeners are notified of the initialization context before filters or servlets on the network Application for initialization.
- Parameters:
-
sce
– ServletContextEvent that contains ServletContext this is initialized
Context Destroyed
- Receive notification that ServletContext will be available soon done below.
All servlets and filters have already been destroyed before notifying the ServletContextListener associated with the context Destruction.
- Parameters:
-
sce
– ServletContextEvent with ServletContext what is destroyed
Report a bug or feature
Copyright © 2009-2011, Oracle Corporation and / or its affiliates. All rights reserved. Use is governed by the terms of the license.
Created on February 10, 2011 at 12:41 pm
Sometimes the public needs to call code when starting and closing a web application. There are several ways to do this, but in this article I will cover the current ServletContextListener
.
This is how it works
PC running slow?
ASR Pro is the ultimate solution for your PC repair needs! Not only does it swiftly and safely diagnose and repair various Windows issues, but it also increases system performance, optimizes memory, improves security and fine tunes your PC for maximum reliability. So why wait? Get started today!
ServletContextListener
is a graphical user interface that is notified using the ServletContext lifecycle buttons. He offers twobut the method.
-
contextInitialized
– Recommended if the web application launches our own initialization. This is called before the filters and servlets are initialized. -
contextDestroyed
Triggered if the ServletContext still needs to be destroyed. This will be called after all servlets and the filtering system are destroyed. See
Let’s see how the ServletContextListener
implementation might look like.
package com.deadcoderising;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class Implements examplecontextlistener ServletContextListener @ Crush open to public void contextInitialized (ServletContextEvent servletContextEvent) System.out.println ("Let's start!"); @ Crush public void contextDestroyed (ServletContextEvent servletContextEvent) System.out.println ("Stop!");
How to add it to an extended web application
There are two options to consider to add it to your web application. You can specify any of our listeners in web.xml
.
com.deadcoderising.ExampleContextListener
Or you can usually use a notation @WebListener
. This annotation seems to have been introduced in Servlet 3.0 and ensures that the web application provides your ServletContextListener
implementation. Class
@weblistenerpublic ExampleContextListener implements ServletContextListener // ...
The listener sits on the truth , get a notification there and wait for the specified event to happen, perhaps “capture” the event and fire your own event.
Problem
You want to initialize the index connection pool before the web application starts. Does a “main () method” usually exist in a web application environment?
Solution
ServletContextListener
is what you need, it will cost your code to run your web application.
1. Example
In this example, we will show you in servletcontextlistener how to create a fancy listener class by implementing a ServletContextListener
that will run your code before starting the web applications.
1.1 Create a class and ServletContextListener
activate the interface.
MyAppServletContextListener.java
Com.mkyong.listener package;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class MyAppServletContextListener ServletContextListener@ Crushpublic implements void contextDestroyed (ServletContextEvent arg0)System.out.println ("ServletContextListener destroyed"); // Do this before the web application starts without a doubt@ Crushpublic contextInitialized (ServletContextEvent void arg0)System.out.println ("ServletContextListener started");
1.2 Include it in your descriptor deployment.
web.xml
com.mkyong.listener.MyAppServletContextListener
1.3 Start Tomcat ….
// ...December 2, 10:11:46 2009 org.apache.catalina.core.StandardEngine startINFO: Start the servlet engine: Apache Tomcat / 6.0.20ServletContextListener ran <-------------- your code here, before our command app --->Dec 2, 10:11:46, 2009 org.apache.coyote.http11.Http11ProtokollstartINFO: Running Coyote HTTP / 1.1 on http-8080// ...INFO: server start in 273 ms
Note
For Servlet 3.0 container.x you can annotate a listener containing @WebListener
without having to declare itduring web .xml
.
Comments
Improve the speed of your computer today by downloading this software - it will fix your PC problems.
What is a servlet context listener?
ServletContextListener is an interface that seems to be notified of changes in the ServletContext lifecycle. He offers two methods. contextInitialized – Injected when the web application starts its own initialization. This is called before the actual initialization of many filters and servlets.
What is the purpose of events and listeners in servlets?
Typical Event Listener Scenario This listener can work like this: the listener is notified when the application starts. The application connects to the database and even stores the connection object in the real servlet context. Servlets use a database connection to perform SQL operations.
Fehlerbehebung Mit Einem Aussagekräftigen Servlet-Kontext-Listener
Problemen Oplossen Met Het Produceren Van Een Servlet-contextluisteraar
Jak Rozwiązać Za Pomocą Detektora Kontekstu Serwletu
Hur Du Hjälper Dig Att Felsöka Med En Servlet -kontextlyssnare
Как устранить неполадки при использовании прослушивателя контекста сервлета
Como Fazer Trobleshoot E Consertar Usando Um Ouvinte De Contexto De Servlet
서블릿 컨텍스트 리스너를 사용하여 문제를 해결하는 방법
Comment Résoudre Les Problèmes De Sélection D’un écouteur De Contexte De Servlet
Cómo Solucionar Problemas Utilizando Un Detector De Contexto De Servlet