ServletContextListener
--------------------------------
We can't very well
stuff 'object' into an XML DD. We can do this by Listeners if, we want objects
those required by any resource of app.
If we want to run
some code before the rest of the app service a client, then we have context
init parameters that we can get and use.
Methods in
ServletContextListener
-------------------------------------------------
contextInitialized(ServletContextEvent)
contextDestroyed(ServletContextEvent)
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>servletContextListenerEg</display-name> <servlet> <description></description> <display-name>ListenerTestSample</display-name> <servlet-name>ListenerTestSample</servlet-name> <servlet-class>com.samples.ctx.ListenerTestSample</servlet-class> </servlet> <servlet-mapping> <servlet-name>ListenerTestSample</servlet-name> <url-pattern>/ListenerTestSample</url-pattern> </servlet-mapping> <context-param> <param-name>network</param-name> <param-value>Vodafone</param-value> </context-param> <listener> <listener-class>com.samples.ctx.MyFirstServletContextListener</listener-class> </listener> </web-app>
Network.java
package com.samples.ctx; public class Network { private String network; public Network(String network) { this.network = network; } public String getNetwork() { return network; } }
MyFirstServletContextListener.java
package com.samples.ctx; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * Application Lifecycle Listener implementation class MyFirstServletContextListener * @author S L N V Praveen */ public class MyFirstServletContextListener implements ServletContextListener { /** * Default constructor. */ public MyFirstServletContextListener() { // TODO Auto-generated constructor stub } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent event) { ServletContext sctx = event.getServletContext(); String network = sctx.getInitParameter("network"); Network nw = new Network(network); sctx.setAttribute("network", nw); } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent event) { // TODO Auto-generated method stub } }
ListenerTestSample.java
package com.samples.ctx; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ListenerTestSample */ public class ListenerTestSample extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); Network nw = (Network) getServletContext().getAttribute("network"); out.println("The Network is : " nw.getNetwork()); } }
Start the Tomcat and hit the below url:
http://localhost:8080/servletContextListenerEg/ListenerTestSample
No comments:
Post a Comment