This section contains the Context Listener Example .
Context Listener Example
Context Listener deals with the servlet context. For servlet context events, the event listener classes can receive notification when the Web Application is deployed or is being undeployed (or when Server shuts down), and when attributes are added, removed, or replaced.
Example
SetDriver.java
public class SetDriver { String driver; String url; String username; String password; public SetDriver(){} public SetDriver(String d,String u,String user,String pass) { driver=d; url=u; username=user; password=pass; } }
CreateConnection.java(context listener class)
import javax.servlet.*; public class CreateConnection implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { ServletContext ctx=event.getServletContext(); String dname=ctx.getInitParameter("driver"); String urladd=ctx.getInitParameter("url"); String uname=ctx.getInitParameter("username"); String pass=ctx.getInitParameter("password"); SetDriver con=new SetDriver(dname,urladd,uname,pass); ctx.setAttribute("ConnectionDesc",con); System.out.println("Context initialized"); } public void contextDestroyed(ServletContextEvent event) { System.out.println("Context destroyed"); } }
FetchData.java
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class FetchData extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); SetDriver cn = (SetDriver) getServletContext().getAttribute( "ConnectionDesc"); if (cn == null) { out.println("<html><body><b> Connection not possible</b>"); } else { try { Class.forName(cn.driver); Connection con = DriverManager.getConnection(cn.url, cn.username, cn.password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from studentrecord"); out.println("<html><body bgcolor=yellow>"); out.println("<br><b>Student Details</b><br><table border=1>"); while (rs.next()) { out.println("<tr><td>" + rs.getInt(1) + "</td><td>" + rs.getString(2) + "</td>"); } out.println("</table>"); out.println("</body></html>"); con.close(); } catch (Exception e) { System.out.println(e); } } } }
web.xml configuration
<servlet> <servlet-name>FetchData</servlet-name> <servlet-class>FetchData</servlet-class> </servlet> <servlet-mapping> <servlet-name>FetchData</servlet-name> <url-pattern>/servlet/FetchData</url-pattern> </servlet-mapping> <listener> <listener-class>CreateConnection</listener-class> </listener> <context-param> <param-name>driver</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </context-param> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://192.168.10.13:3306/ankdb</param-value> </context-param> <context-param> <param-name>username</param-name> <param-value>root</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>root</param-value> </context-param>
Output
When you execute FetchData.java servlet , you will get following output :
[ 0 ] Comments