📄 hitcounterfilter.java
字号:
/*
* @author : Reghu
* @Version : 1.0
*
* Development Environment : Oracle 9i JDeveloper
* Name of the File : HitCounterFilter.java
* Creation/Modification History :
*
* Reghu 21-march-2002 Created
*
*/
package oracle.otnsamples.vsm;
// Java IO files
import java.io.IOException;
// Servlet Filter classes
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
// Servlet related classes
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* This class is implemented as a Servlet Filter class to track the number of
* hits by users. The hits are incremented for every new session.
*
* @author Reghu
* @version 2.0
*/
public final class HitCounterFilter implements Filter {
public static int counter;
/** Hit Counter */
private FilterConfig filterConfig = null; // flter configuration
/**
* The servlet container invokes this method before the filter goes
* into service, and sets the filter's configuration object. The method
* initializes the counter to the value specified in the init param in
* web.xml
*
* @param <b>config</b> The filter config passed by the servlet engine
*
* @throws <b>ServletException</b> if any error in initializing the filter
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
try {
counter = Integer.parseInt(filterConfig.getInitParameter("Counter"));
} catch(Exception ex) {
counter = 0;
}
}
/**
* This method performs the actual filtering work .In its doFilter() method,
* each filter receives the current request and response, as well as a
* FilterChain containing the filters that still must be processed. If the
* session is a new one, the counter is incremented and the control is
* passed down the filter chain
*
* @param <b>request</b> The servlet request
* @param <b>response</b> The servlet response
* @param <b>chain</b> Object representing the chain of all filters
*
* @throws <b>ServletException</b> If there was an error while checking the
* role privileges
* @throws <b>IOException</b>
*/
public void doFilter(
ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
if(filterConfig == null) {
return;
}
// Get the session object
HttpSession session = ((HttpServletRequest) request).getSession();
// If its a new request, its a new user to the site
if(session.getAttribute("_counter") == null) {
// Get the Counter stored as a context attribute.
counter++;
session.setAttribute("_counter", "c");
}
// Invokes the doFilter of the filter chain object
chain.doFilter(request, response);
}
/**
* Called by the web container to indicate to a filter that it is being taken
* out of service. This method is only called once all threads within the
* filter's doFilter method have exited or after a timeout period has
* passed. After the web container calls this method, it will not call the
* doFilter method again on this instance of the filter. This method gives
* the filter an opportunity to clean up any resources that are being held
* (for example, memory, file handles, threads) and make sure that any
* persistent state is synchronized with the filter's current state in
* memory. The method sets the filterConfig to null.
*/
public void destroy() {
this.filterConfig = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -