Monitor and filter notes

8. listener 
============================================== === 
two methods register a listener: 
1. @ WebListener annotation 
@WebListener 
public class {} ListenerClass the implements listenerInterface 

2.web.xml using a listener element. 
<listener> 
    <listener-class> Fully-qualified listener class </ listener-class> 
</ listener> 


the Servlet the Context Listener - implements the interface of ServletContextListener 
1.ServletContextListener: initialization and deconstruction of ServletContext respond. 
    void contextInitialized (ServletContextEvent event); --- > The method of rewriting main 
    void the contextDestroyed (ServletContextEvent Event); 
2.ServletContextAttributeListener: 
    void attributeAdded (ServletContextAttributeEvent SCAE);
    attributeRemoved void (ServletContextAttributeEvent SCAE); 
    void attributeReplaced (ServletContextAttributeEvent SCAE); 
    
the Session Listener - Interface implement the HttpSessionListener 
1.HttpSessionListener 
2.HttpSessionActivationListener 
3.HttpSessionAttributeListener 
4.HttpSessionBindingListender 

user counter instance 

ServletRequest listener - implement interface ServletRequestListener 
1.ServletRequestListener 
2. ServletRequestAttributeListener 
3.AsyncListener 

user access time 

9. The filter 
======================================= ========== 
filter means intercepts the request, and passed to the requested resource for a ServletResponse ServletRequest or object. 

Filter API 
include: Filter, FilterConfig and FilterChain.
Javax.servlet.Filter filter must implement the interface. 3 method: 
		<filter-name> 
void the init (the FilterConfig FilterConfig);
void the doFilter (the ServletRequest REQ, the ServletResponse RESP, the FilterChain FC); 
void Destory () 

method FilterConfig interface: 
public String getFilterName (); 
public the ServletContext GetServletContext (); 
public String the getInitParameter (String name); 
public the Enumeration <String> the getInitParameterNames () ; 

method FilterChain interface: 
public void the doFilter (the ServletRequest Request, the ServletResponse Response); 

configuring filters method: 
1. @ WebFilter annotation 
2.web.xml used filter element and the filter-mapping 
	<filter> 
		<filter-name> XXX < / filter-name> 
		<filter-class> yyy </ filter-class> 
	</ filter> 
	<filter-Mapping> 
		<URL-pattern> / * </ URL-pattern> 
	</ filter-mapping> 

Example 1: Log filter
Example 2: Images protection filter 
Example 3: Download count filter 

order filters: web.xml in order to write the call. 


10. The application design 
configuration JNDI Datasource steps: 
1. Copy the JDBC driver mysql-connector-java-5.1.44- bin.jar to the tomcat / lib / directory. 
2.Context configuration: the META-INF / context.xml 
<the Context> 
  <the Resource name = "JDBC / the TestDB" the auth = "Container" type = "the javax.sql.DataSource" 
               maxTotal = "100" maxIdle = "30" = maxWaitMillis "10000" 
               username = "the root" password = "the root" driverClassName = "com.mysql.jdbc.Driver" 
               URL = "JDBC: MySQL: //192.168.15.45: 3307 / a_spider_test" /> 
</ the Context> 
3.Web .xml configuration: the WEB-INF / the web.xml 
  <Resource-REF> 
      <
      <res-auth>Container</res-auth>
  </resource-ref>
4.编写代码:
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/TestDB");
Connection con = dataSource.getConnection();

  

Guess you like

Origin www.cnblogs.com/zhangkaipc/p/11778394.html