2016-11-21 24 views
-1

我使用Servlet在部署應用程序時查看服務器上的流量。如果在部署應用程序之前服務器上有任何流量,則返回false,否則返回true。到目前爲止它工作正常,但我讀了關於預處理Servlet過濾器。有沒有辦法像使用servlet過濾器的預處理一樣?使用Servlet捕獲服務器中的流量

Java類:

public class RESTActivityServlet extends HttpServlet { 

private static final long serialVersionUID = -4971654546064688463L; 

private Long countServerTraffic; 
public static final String REQUEST_COUNT_1H = "RequestCount_1h"; 
public static final String REQUEST_COUNT_15M = "RequestCount_15m"; 
public static final String REQUEST_COUNT_1M = "RequestCount_1m"; 
public static final String REQUEST_COUNT_15S = "RequestCount_15s"; 
public static final String REQUEST_COUNT_1S = "RequestCount_1s";  
public static final String ACTIVITY_JSP_INTERVAL = "interval"; 

public void init(ServletConfig config) throws ServletException { 
    countServerTraffic = 0L; 
} 

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    long defaultTime = 60; 
    if(request.getParameter(ACTIVITY_JSP_INTERVAL) != null) { 
     defaultTime = Long.valueOf(request.getParameter(ACTIVITY_JSP_INTERVAL)); 
    } 
    PrintWriter out = response.getWriter(); 
    response.setContentType("text/plain"); 
    response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate"); 
    response.setHeader("Pragma", "No-Cache"); 
    out.print(hasServerHadTrafficInLastTimePeriod(defaultTime)); 
    countServerTraffic++; 
} 

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { 
    doGet(request, response); 
} 

public Boolean hasServerHadTrafficInLastTimePeriod(long time) { 
    try { 
     String[] attrs = null; 

     if(time > 0 && time <=1) { 
      attrs = new String[] { REQUEST_COUNT_1S }; 
     } else if(time > 1 && time <= 15) { 
      attrs = new String[] { REQUEST_COUNT_1S, REQUEST_COUNT_15S }; 
     } else if(time > 15 && time <= 60) { 
      attrs = new String[] { REQUEST_COUNT_1S, REQUEST_COUNT_15S, REQUEST_COUNT_1M }; 
     } else if(time > 60 && time <= 900) { 
      attrs = new String[] { REQUEST_COUNT_1S, REQUEST_COUNT_15S, REQUEST_COUNT_1M, REQUEST_COUNT_15M }; 
     } else if(time > 900 && time <= 3600) { 
      attrs = new String[] { REQUEST_COUNT_1S, REQUEST_COUNT_15S, REQUEST_COUNT_1M, REQUEST_COUNT_15M, REQUEST_COUNT_1H }; 
     } else { 
      attrs = new String[] { "RequestCount_total" }; 
     } 

     for(String attr : attrs) { 
      if(countServerTraffic instanceof Long) { 
       if(((Long)countServerTraffic).longValue() != 0l) { 
        System.out.println("Attribute " + attr + " found traffic of " + countServerTraffic); 
        return true; 
       } 
      } 
     } 

     return false; 
    } catch (Exception ex) { 
     System.out.println("Exception in Activity Servlet: " + ExceptionUtils.getStackTrace(ex)); 
     return false; 
    } 
} 

}

的web.xml:

<servlet> 
    <display-name>ActivityServlet</display-name> 
    <description>ESign Activity</description> 
    <servlet-name>ActivityServlet</servlet-name> 
    <servlet-class>com.mercuryinsurance.esignature.activity.RESTActivityServlet</servlet-class> 
    <init-param> 
     <param-name>appName</param-name> 
     <param-value>ESignatureUIWAR</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>ActivityServlet</servlet-name> 
    <url-pattern>/Activity</url-pattern> 
</servlet-mapping> 

感謝。

+0

乘坐看看Servlet過濾器。它應該非常簡單。 – 2016-11-21 21:58:24

+0

總而言之,是的。太寬泛。 – EJP

回答

0

Long不是線程安全的,所以你不能用它作爲你的櫃檯一個servlet實例變量,所以你需要使用AtomicLong(這是線程安全的),如下圖所示:

public class RESTActivityServlet extends HttpServlet { 

    private static final long serialVersionUID = -4971654546064688463L; 

    AtomicLong countServerTraffic = new AtomicLong(0);//AtomicLong variable which thread safe 

    public static final String REQUEST_COUNT_1H = "RequestCount_1h"; 
    public static final String REQUEST_COUNT_15M = "RequestCount_15m"; 
    public static final String REQUEST_COUNT_1M = "RequestCount_1m"; 
    public static final String REQUEST_COUNT_15S = "RequestCount_15s"; 
    public static final String REQUEST_COUNT_1S = "RequestCount_1s";  
    public static final String ACTIVITY_JSP_INTERVAL = "interval"; 

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     countServerTraffic.getAndIncrement();//increment 

     long defaultTime = 60; 
     if(request.getParameter(ACTIVITY_JSP_INTERVAL) != null) { 
      defaultTime = Long.valueOf(request.getParameter(ACTIVITY_JSP_INTERVAL)); 
     } 
     PrintWriter out = response.getWriter(); 
     response.setContentType("text/plain"); 
     response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate"); 
     response.setHeader("Pragma", "No-Cache"); 
     out.print(hasServerHadTrafficInLastTimePeriod(defaultTime)); 
    } 

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { 
     doGet(request, response); 
    } 

    public Boolean hasServerHadTrafficInLastTimePeriod(long time) { 
     //add your code 
    } 
} 
+0

這就像Long的替代品。我正在嘗試將過濾器作爲預過程來實施。在部署應用程序之前檢查服務器上是否存在任何請求。 – Mike