-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>
感謝。
乘坐看看Servlet過濾器。它應該非常簡單。 – 2016-11-21 21:58:24
總而言之,是的。太寬泛。 – EJP