public
interface ServletContextListener extends
java.util.EventListener
Implementations
of this interface receive notifications about changes to the servlet context of
the web application they are part of. To receive notification events, the
implementation class must be configured in the deployment descriptor for the
web application.
Methods
void
|
contextDestroyed(ServletContextEvent sce)
Notification that the servlet context is about to be shut down. |
void
|
contextInitialized(ServletContextEvent sce)
Notification that the web application initialization process is starting. |
Web.xml
servlet_demo index.html com.jsl.servlet.StoreUsers viewUsers com.jsl.servlet.ViewUserList viewUsers /viewUsers
StoreUsers.java
package com.jsl.servlet; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class StoreUsers implements ServletContextListener { Listlist=new ArrayList (); @Override public void contextDestroyed(ServletContextEvent event) { System.out.println("Context Detsroy method invoked"); list=null; } @Override public void contextInitialized(ServletContextEvent event) { System.out.println("Context initilized method invoked"); list.add("Krish"); list.add("Balu"); list.add("Mani"); ServletContext context=event.getServletContext(); context.setAttribute("users", list); } }
ViewUserList.java
package com.jsl.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ViewUserList extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context=getServletContext(); resp.setContentType("text/html"); PrintWriter out=resp.getWriter(); out.print("<html><head><title>Users list</title></head>"); out.print("<body>"); out.print("<h3> users list is </h3>"); out.print("<hr>"); Listusers=(List ) context.getAttribute("users"); for(String user:users){ out.print("<h5>"+user+"</h5>"); } out.print("</body>"); out.print("</html>"); } }
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>Insert title here Users List </html>
http://localhost:8083/servlet_demo/
http://localhost:8083/servlet_demo/viewUsers
Same example in servlet 3.0
web.xml
servlet_demo index.html
StoreUsers.java
package com.jsl.servlet; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; public class StoreUsers implements ServletContextListener { Listlist=new ArrayList (); @Override public void contextDestroyed(ServletContextEvent event) { System.out.println("Context Detsroy method invoked"); list=null; } @Override public void contextInitialized(ServletContextEvent event) { System.out.println("Context initilized method invoked"); list.add("Krish"); list.add("Balu"); list.add("Mani"); ServletContext context=event.getServletContext(); context.setAttribute("users", list); } }
ViewUserList.java
package com.jsl.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/viewUsers") public class ViewUserList extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context=getServletContext(); resp.setContentType("text/html"); PrintWriter out=resp.getWriter(); out.print("<html><head><title>Users list</title></head>"); out.print("<body>"); out.print("<h3> users list is </h3>"); out.print("<hr>"); Listusers=(List ) context.getAttribute("users"); for(String user:users){ out.print("<h5>"+user+"</h5>"); } out.print("</body>"); out.print("</html>"); } }
ServletContextAttributeListener
public
interface ServletContextAttributeListener
extends java.util.EventListener
Implementations
of this interface receive notifications of changes to the attribute list on the
servlet context of a web application. To receive notification events, the
implementation class must be configured in the deployment descriptor for the
web application.
package com.jsl.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/users") public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context=getServletContext(); context.setAttribute("user1", "Krish"); context.setAttribute("user2", "Krish"); context.setAttribute("user3", "Balu"); context.removeAttribute("user2"); context.setAttribute("user3", "Balakrishna"); } }
package com.jsl.servlet; import java.util.Date; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.annotation.WebListener; @WebListener public class UserListner implements ServletContextAttributeListener{ @Override public void attributeAdded(ServletContextAttributeEvent event) { System.out.println("The attribute is added "+event.getName()+" "+new Date()); } @Override public void attributeRemoved(ServletContextAttributeEvent event) { System.out.println("The attribute is removed "+event.getName()+" "+new Date()); } @Override public void attributeReplaced(ServletContextAttributeEvent event) { System.out.println("The attribute is replaced "+event.getName()+" "+new Date()); } }http://localhost:8083/servlet_demo/users
The servlet console display the notifications
No comments:
Post a Comment