1. 程式人生 > >JSP流量監控指令碼使用方法

JSP流量監控指令碼使用方法

百度不到JSP的流量監控指令碼,自己寫一個吧,1.0版本,後續更新會封裝一下;功能:儲存請求頭和表單資訊並列印
使用方法:
1 把web.xml配置為這個指令碼的路徑


2 把全域性變數設定為你要儲存的txt檔案路徑

效果:


程式碼如下: 

import java.io.File;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

  

public class liu extends HttpServlet {


    public static String path = "E:/Test/log.txt";
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	

	
	     public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
	         response.setCharacterEncoding("UTF-8");//設定將字元以"UTF-8"編碼輸出到客戶端瀏覽器
 //通過設定響應頭控制瀏覽器以UTF-8的編碼顯示資料
	        response.setHeader("content-type", "text/html;charset=UTF-8");
	         PrintWriter out = response.getWriter();
	         @SuppressWarnings("unchecked")
//           遍歷獲取表單值
           Enumeration<String> em = request.getParameterNames();
           while (em.hasMoreElements()) {
	            String postname = (String)em.nextElement();
	            String postvalue = request.getParameter(postname);
              String postvalues = postname+":"+postvalue;
              System.out.println(postvalues);


     
             
              
              BufferedWriter out1 = null;
              try {
              out1 = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream(path, true)));
              out1.write(postvalues+"\r\n");
             
              } catch (Exception e) {
              e.printStackTrace();
              } finally {
            	  
                  
              try {
              out1.close();
              } catch (IOException e) {
              e.printStackTrace();
              }
              }
                          

           }
	        
	         
//	         在頁面上回顯資料
			Enumeration<String> reqHeadInfos = request.getHeaderNames();//獲取所有的請求頭
	        out.write("獲取到的客戶端所有的請求頭資訊如下:");
	        out.write("<hr/>");

//	        遍歷獲取請求頭資訊
	        while (reqHeadInfos.hasMoreElements()) {
	             String headName = (String) reqHeadInfos.nextElement();
//根據請求頭的名字獲取對應的請求頭的值
	             String headValue = request.getHeader(headName);
	             out.write(headName+":"+headValue);
	             out.write("<br/>");

	           String string = headName+":"+headValue;
	           
               FileWriter fw = null;
	             try {
 //  存入指定路徑,如果檔案存在,則追加內容;如果檔案不存在,則建立檔案
	             File f=new File("E:/Test/log.txt");
	             fw = new FileWriter(f, true);
	             } catch (IOException e1) {
	             e1.printStackTrace();
	             }
	             PrintWriter pw = new PrintWriter(fw);
	             pw.println(string);
	             
	             pw.flush();
	             try {
	            
	             fw.flush();
	             pw.close();
	             
	             fw.close();
	            
	             } catch (IOException e1) {
	             e1.printStackTrace();
	             }
	             
	             }
	        
	        FileWriter fw = null;
            try {
            //如果檔案存在,則追加內容;如果檔案不存在,則建立檔案
            File f=new File("path");
            fw = new FileWriter(f, true);
            } catch (IOException e) {
            e.printStackTrace();
            }
            PrintWriter pw = new PrintWriter(fw);
//  加上時間
            Date date= new Date(System.currentTimeMillis());
        	   String pattern="yyyy/MM/dd HH時mm分ss秒";
        			   SimpleDateFormat sdf= new SimpleDateFormat(pattern);
        	   String datestr=sdf.format(date);// format  為格式化方法
        	   pw.write(datestr);

            pw.flush();
            try {
           
            fw.flush();
            pw.close();
            
            fw.close();
           
            } catch (IOException e) {
            e.printStackTrace();
            }
   
 }
	       
	         
	 
	   
	 
	     public void doPost(HttpServletRequest request, HttpServletResponse response)
	             throws ServletException, IOException {
	         doGet(request, response);
	     }
	 
}