使用Servlet

时间:2016-11-21 21:38:34

标签: java servlets

我在部署应用时使用Servlet来查看服务器上的流量。如果在部署应用程序之前服务器上有任何流量,则返回false,否则true。到目前为止它工作正常,但我读到了有关预处理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> 

感谢。

1 个答案:

答案 0 :(得分:0)

message不是线程安全的,所以你不能将它用作你的计数器的servlet实例变量,所以你需要使用message_text(这是线程安全的),如下所示: / p>

Long