• Post Reply Bookmark Topic Watch Topic
  • New Topic

My example with ServletContextListener and ServletContextAttrib.  RSS feed

Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought my example may help anybody who are studying for the exam and i wanted to share it with you.
1.MyServlet --> Servlet to print my firstname and lastname and implements also the listener interface
ServletContextAttributeListener. I did it to check if the attributeAdded() method will be called or not.
2.MyServletContextListener --> this is a listener to add and remove attribute (my lastname) from context.
What i'm doing ? i explained with comments in my code.
My aim is to print "Engin OKUCU" , 'Engin' is defined as parameter in the web.xml and 'OKUCU'
is defined in my MyServletContextListener wich will add it in the application's context when
the container is starting.
1. start the container.
2. public void contextInitialized(ServletContextEvent sce) is called as MyServletContextListener
implements ServletContextListener.
3. System.out.println("contextInitialised() is called")
4. sce.getServletContext().setAttribute("lastname",lastname)
5. public void attributeAdded(ServletContextAttributeEvent scab) is called as a context parameter
has been added to the context.
6. System.out.println("attributeAdded is called and the lastname is : "+lastname) --> output for lastname is null
7. System.out.println("this is called after attributeAdded() method")
8. init(ServletConfig config) is called to instantiate the servlet by getting my firstname from

9. call the servlet by making a request : http://localhost:8080/epfc/MyServlet
--> output for lastname is OKUCU
10. shutdown the container
11. the attribute is removed from context.--> sce.getServletContext().getAttribute("lastname")

--------- web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"





package info;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class MyServlet extends HttpServlet implements ServletContextAttributeListener
static String firstname = null;
String value = null;
String lastname = null ;
int i=0;

public void init(ServletConfig config) throws ServletException
ServletContext context = config.getServletContext() ;
// we retrieve the attribute added to the context
lastname = (String)context.getAttribute("lastname"); // the value of lastname is now 'OKUCU'
value = config.getInitParameter("firstname");
if(value == null)
firstname ="No name is specified";
firstname = value;


public void attributeAdded(ServletContextAttributeEvent scab)
// it prints null to the output because the servlet is not yet instantiated
System.out.println("attributeAdded is called and lastname is : "+lastname);


public void attributeRemoved(ServletContextAttributeEvent scab) {}

public void attributeReplaced(ServletContextAttributeEvent scab) {}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException

PrintWriter out = res.getWriter();
// firstname is Engin (defined in web.xml) and lastname is OKUCU (added by setAttribute() to the context of the web app.)


package info;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class MyServletContextListener implements ServletContextListener
String lastname = "OKUCU";

public void contextInitialized(ServletContextEvent sce)
System.out.println("contextInitialised() is called");
System.out.println("this is called after attributeAdded() method");


public void contextDestroyed(ServletContextEvent sce)
System.out.println("Context destroyed");

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!