JSP Listener Listener (i) ———————— Servletcontextlistener

Source: Internet
Author: User

Summary:

        when web applied to the web web web application is started East, web app is stopped, user session start, user session end, user request arrives, etc... these web web Anything that happens inside the container.

Servletcontextlistener

The common listener has

Servletcontextlistener: Used to listen for Web app startup and shutdown

Contextinitialized (servletcontextevent arg0) executes when the Web app is launched

Contextdestroyed (Servletcontextevent arg0) executes when the Web app is started off

There are two ways to configure listening:

The first is the way web annotations are

The second is to configure the XML file

1  PackageCom.huayu.listener;2 3 Importjavax.servlet.ServletContextEvent;4 ImportJavax.servlet.ServletContextListener;5 6  Public classListener1Implementsservletcontextlistener{7 8 @Override9      Public voidcontextdestroyed (servletcontextevent arg0) {Ten         //TODO auto-generated Method Stub OneSystem.out.println ("========= app off ============"); A     } -  - @Override the      Public voidcontextinitialized (servletcontextevent arg0) { -         //TODO auto-generated Method Stub -System.out.println ("= = = App start ========="); -     } +  -}

Xml

1  <listener>2           <listener-class>com.huayu.listener.listener1</listener-  Class>3   </listener>

The following shows:

Write a Servletcontextlistener listener when the Web app launcher is linked to the orcl database , in the JSP the data in the EMP table is printed as a table in the page , disconnected and orcl when the Web app is closed Links to databases

  

1  PackageCom.huayu.listener;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 Importjava.sql.SQLException;7 8 ImportJavax.servlet.ServletContext;9 Importjavax.servlet.ServletContextEvent;Ten ImportJavax.servlet.ServletContextListener; One  A  Public classListener1Implementsservletcontextlistener{ -      - @Override the      Public voidcontextdestroyed (servletcontextevent arg0) { -          -ServletContext application=Arg0.getservletcontext (); -Connection conn= (Connection) application.getattribute ("Connoracle"); +         if(conn!=NULL){ -              +             Try { A conn.close (); at}Catch(SQLException e) { -                 //TODO auto-generated Catch block - e.printstacktrace (); -             } -         } -      in     } -  to @Override +      Public voidcontextinitialized (servletcontextevent arg0) { -         //TODO auto-generated Method Stub theString driver= "Oracle.jdbc.driver.OracleDriver"; *String url= "Jdbc:oracle:thin: @localhost: 1521:orcl"; $String user= "Scott";Panax NotoginsengString pass= "Ztoracle"; -          theServletContext application=Arg0.getservletcontext (); +          A         Try { the Class.forName (driver); +Connection conn=drivermanager.getconnection (URL, user, pass); -Application.setattribute ("Connoracle", conn); $              $}Catch(Exception e) { -             Throw Newruntimeexception (); -              the              -         }Wuyi          the     } -  Wu}

1<% @pageImport= "Java.sql.ResultSetMetaData"%>2<% @pageImport= "Java.sql.ResultSet"%>3<% @pageImport= "Java.sql.PreparedStatement"%>4<% @pageImport= "Java.sql.Connection"%>5<%@ page language= "Java"Import= "java.util.*" pageencoding= "GBK" contenttype= "text/html; CHARSET=GBK "%>6<%7String Path =Request.getcontextpath ();8String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";9%>Ten  One<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > A - -<base href= "<%=basePath%>" > the      -<title>my JSP ' index.jsp ' starting page</title> -<meta http-equiv= "Pragma" content= "No-cache" > -<meta http-equiv= "Cache-control" content= "No-cache" > +<meta http-equiv= "Expires" content= "0" > -<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > +<meta http-equiv= "description" content= "This is my page" > A<!-- at<link rel= "stylesheet" type= "Text/css" href= "Styles.css" > -- - -    -<body> -<% inConnection conn= (Connection) application.getattribute ("Connoracle"); -PreparedStatement ps=conn.preparestatement ("SELECT * from emp"); toResultSet rs=ps.executequery (); +ResultSetMetaData rsm=Rs.getmetadata (); -         intCount=Rsm.getcolumncount (); the%> *<table border= "1" > $<%Panax Notoginseng              while(Rs.next ()) { -%> the<tr> +<% A                  for(inti=0;i<count;i++){ the              +%> -<td> $<% $Rs.getstring (i+1); -%> -              the              -</td>Wuyi<%}%> the</tr> -<%}%> Wu</table> -      About</body> $

JSP Listener Listener (i) ———————— Servletcontextlistener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.