Servlet – Context Event and Context Listener
Last Updated :
24 Mar, 2022
ServletContextEvent class provides alerts/notifications for changes to a web application’s servlet context. ServletContextListener is a class that receives alerts/notifications about changes to the servlet context and acts on them. When the context is initialized and deleted, the ServletContextListener is utilized to conduct crucial tasks. In a nutshell, ServletContextEvent and ServletContextListener function in tandem; anytime the ServletContext changes, ServletContextEvent broadcasts a notice, which is received by ServletContextListener, which then performs various duties based on the message.
Constructor of ServletContextEvent Class
ServletContextEvent(ServletContext e)
In the ServletContextEvent class, there is just one constructor. After the ServletContext instantiation, the web container produces an instance of ServletContextEvent.
Methods of ServletContextEvent
public ServletContext getServletContext()
Note: It returns the instance of ServletContext.
Methods of ServletContextListener Interface
Method
|
Action Performed
|
void contextInitialized(ServletContextEvent e) |
When the application is first initialized, this method is called. |
void contextDestroyed(ServletContextEvent e) |
When the application is destroyed, this method is called. |
Example: ServletContextEvent and ServletContextListener
In this example, we will have to create a table named counter with a column named pageview to save the number of pageviews. by using this database record we will find out the total number of page views.
A. File: index.html
HTML
< a href = "CounterGfg" >Total Page views GeeksforGeeks</ a >
|
B. File: web.xml
XML
< web-app >
< listener >
< listener-class >MyListenerGfg</ listener-class >
</ listener >
< servlet >
< servlet-name >CounterGfg</ servlet-name >
< servlet-class >CounterGfg</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name >CounterGfg</ servlet-name >
< url-pattern >/CounterGfg</ url-pattern >
</ servlet-mapping >
</ web-app >
|
C. File: MyListenerGfg.java
Java
import java.sql.*;
import javax.servlet.*;
public class MyListenerGfg implements ServletContextListener {
ServletContext ctx;
Connection con;
Statement s;
PreparedStatement ps;
ResultSet rs;
int count;
public void contextInitialized(ServletContextEvent sce) {
try {
Class.forName( "com.mysql.jdbc.Driver" );
s = con.createStatement();
rs = s.executeQuery( "select pageview from counter" );
while (rs.next()) {
count = rs.getInt( 1 );
}
ctx = sce.getServletContext();
ctx.setAttribute( "pcount" , count);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent sce) {
try {
ctx = sce.getServletContext();
count = (Integer)ctx.getAttribute( "pcount" );
ps = con.prepareStatement( "update counter set pcount='" + count + "'" );
ps.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
D. File: CounterGfg.java
Java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CounterGfg extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType( "text/html;charset=UTF-8" );
PrintWriter out = response.getWriter();
ServletContext ctx = getServletContext();
Integer count = (Integer)ctx.getAttribute( "pcount" );
out.println(count + ": pageview" );
ctx.setAttribute( "pcount" , ++count);
}
}
|
Now, run ‘index.html’ file on server and this will show the following output
Output:
After clicking on that link following page will be loaded which shows the count of page views means how many times the user visited the page. The page count will be increased every time you refresh or revisit the page.
Share your thoughts in the comments
Please Login to comment...