The @WebListener annotation is used to register a class as a listener of a web application. The annotated class must implement one or more of the following interfaces:

 

Syntax of @WebListener Annotation:

@WebListener([optional description])

 

Attributes of @WebListener Annotation:

Name

Type

Required

Description

value

String

Optional

Description of the listener.

 

Some Examples with @WebListener Annotation:

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionListener;

@WebListener("Session listener for the application")
public class MySessionListener implements HttpSessionListener,
		HttpSessionAttributeListener {

	// overrides required methods here...
}

A great application of using @WebListener is to implement hit counter for Java web applications. Read this tutorial to learn more.

 

Related Java Servlet Annotations:

 

Other Java Servlet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.