天天看點

ServletContext域對象與 ServletContextListener 實作web通路次數統計

1. ServletContext ???翠釜Web?? 涓?涓???瀵矽薄??琚?Web????????Servlet???變韓??

2.ServletContextListener?璐?璐g???? ServletContext ????寤哄????姣??????藉?ㄦ????

3.瀹??闆????:

? ? ?锛?1锛?褰?tomcat???ㄦ?訛?ServletContextListener绗?涓??堕?磋???寤猴?璐?璐g???? ServletContext????寤轟???姣????ㄧ??????ervletContext琚???寤烘?訛?public?void?contextInitialized(((ServletContextEvent?sce))浼?琚?璋????ㄦ?ゆ?規?涓?璇誨??count/count.txt??浠訛??峰??璁塊??浜烘??ount璁℃?幫?骞朵?灏?瑁???ounter瀵矽薄锛? 瀛??ㄥ???ㄥ?涓?涓???ServletContext????涓???

? ?(2) 姣?褰???Servlet??Service(...)?規?琚?璋??ㄦ?訛?灏卞?矽?塊??杩?琛?璁闆???

?(3) 褰?Web?抽???訛?ServletContextListener涓??? public void contextDestroyed( ServletContextEvent sce )?規?琚?璋????杩?琛?璁℃?闆?ㄧ??????瀛??ㄣ??

ServletContextListener??????
	   ServletContextListener????????藉?????ServletContext瀵矽薄?????藉?ㄦ??锛?瀹???涓?灏辨??????Web搴??ㄧ?????藉?ㄦ??锛?
	   (1) Web搴??ㄥ???ㄦ?????抽???訛?浼?????ervletContextEvent浜?浠訛?璇ヤ?浠剁??ervletContextListener?ュ???????ervletContextListener
	      ?ュ?d腑瀹?涔?浜?澶???ServletContextEvent浜?浠剁??涓や釜?規???
	      	1锛?contextInitialized(ServletContextEvent sce);
	      		褰?Servlet瀹瑰?ㄥ???ㄦ??eb搴??ㄤ?璋??ㄨ?ユ?規??? ?跺??瀹瑰?ㄥ??瀵?ilter??濮???锛?Servlet??濮???锛?
	      		
	      	2锛?contextDestroyed(ServletContextEvnet sce);
	      		褰?Servlet瀹瑰?ㄧ?姝?eb搴??ㄨ??ㄦ?ゆ?規????ㄨ??ㄦ?ゆ?規?涔???锛?瀹瑰?ㄤ?????姣???????Servlet??Filter??
	   
	   (2) ??eb.xml涓?娉ㄥ?? ??????istener
	       <listener>
 			<listener-class>com.test1.MyServletContextListener</listener-class>
 		   </listener>		
 		   
 	   (3) 娉ㄥ??????????ttpServletListener浼???瑙???????Servlet????寤轟???姣?
 	   	   public class MyServletContextListener implements ServletContextListener  {


	 *  Web搴??ㄥ???ㄦ?惰??????ilter涔???灏辮?璋???           
@Override
	public void contextInitialized(ServletContextEvent sce) {
	
		System.out.println("helloApp application is Initialized");
		
		//?峰??ServletContext瀵矽薄
		ServletContext context = sce.getServletContext();
		
		BufferedReader reader = null;
		int count = 1;
		//浠???浠朵腑璇誨??璁℃?闆?ㄧ????		try {
			
			String filePath = context.getRealPath("/WEB-INF");
			filePath = filePath+"/count.txt";
			File file = new File( filePath );
			if( file==null || !file.exists() ) {
				System.out.println("??浠朵?瀛???);
			}else {
				System.out.println("??浠跺????);
			}
			
			FileInputStream in = new FileInputStream(file);
			reader = new BufferedReader( new InputStreamReader( in ) );
			String line = reader.readLine();
			if( line != null && line.length()>1 ) {
				System.out.println("line:"+line+"AAAAA"+line.length());
				count = Integer.parseInt(line);
				System.out.println("count: "+count);
			}
			
		}  catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				
				if( reader != null ) {
					reader.close();
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//??寤鴻?℃?闆??浠?浠??峰??涓?涓?瀛??ㄧ???闆?鹼??ㄦ?ュ??濮???Counter瀵矽薄
		Counter counter = new Counter(count);
		//灏?璁℃?闆?ㄤ?瀛???eb??ServletContext??涓?.
		context.setAttribute("Counter", counter);
	}


	 *  Web搴??ㄥ?抽???惰??????ilter琚???姣?涔?????琚?璋???	 
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
	
		//?峰??ServletContext瀵矽薄
		ServletContext context = sce.getServletContext();
		
		Counter counter = (Counter)context.getAttribute("Counter");
		
		//瀛??版??浠朵腑
		if( counter != null ) {
			
			String filePath = context.getRealPath("/WEB-INF");
			filePath = filePath+"/count.txt";
			try {
				
				File file = new File(filePath);
				if( file==null || !file.exists() ) {
					System.out.println("??浠朵?瀛???);
					try {
						file.createNewFile();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					System.out.println("??浠跺????);
				}
				
				FileOutputStream  outputStream =  new FileOutputStream(file);
				BufferedWriter  writer = new BufferedWriter( new OutputStreamWriter(outputStream) );
				try {
					writer.write(counter.getCount());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					
					if( outputStream !=null ) {
						try {
							outputStream.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
           

? ?

*   ServletContext ??瀵矽薄浣跨?ㄦ?渚?锛?
 *   		
		?翠釜Web??璁塊??浜烘?扮?璁?		ServletContext context = getServletContext();
		Counter counter = (Counter)context.getAttribute("Counter");
		
		//绗?涓?娆¤?塊??
		if( counter == null ) {
			counter = new Counter(1);
			context.setAttribute("Counter", counter);
		}
		
		response.setContentType("text/html;charset=GB2312");
		PrintWriter out = response.getWriter();
		out.println("<html><head><title>缁?璁¤?塊??娆℃??lt;/title></head>");
		out.println("<body>");
		out.println("<h1>娆㈣???涓存??绔?锛??ㄦ??绗? "+counter.getCount()+" 涓?璁塊????</h1>");
		out.println("</body>");
		counter.add(1);  //璁℃?闆??1;
           

繼續閱讀