package
com.zj.sample;
import
java.io.IOException;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
public
class SimpleFilter1 implements Filter
{
@SuppressWarnings("unused")
private FilterConfig filterConfig;
public void init(FilterConfig config)
throws ServletException {
this.filterConfig = config;
}
public void doFilter(ServletRequest
request, ServletResponse response,
FilterChain chain) {
try {
System.out.println("Within SimpleFilter1:Filtering the
Request...");
chain.doFilter(request, response);// 把处理发送到下一个过滤器
System.out .println("Within SimpleFilter1:Filtering the
Response...");
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (ServletException se) {
se.printStackTrace();
}
}
public void destroy() {
this.filterConfig = null;
}
} |
package
com.zj.sample;
import
java.io.IOException;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
public
class SimpleFilter2 implements Filter
{
@SuppressWarnings("unused")
private FilterConfig filterConfig;
public void init(FilterConfig config)
throws ServletException {
this.filterConfig = config;
}
public void doFilter(ServletRequest
request, ServletResponse response,
FilterChain chain) {
try {
System.out.println("Within SimpleFilter2:Filtering the
Request...");
chain.doFilter(request, response); // 把处理发送到下一个过滤器
System.out.println("Within SimpleFilter2:Filtering the
Response...");
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (ServletException se) {
se.printStackTrace();
}
}
public void destroy() {
this.filterConfig = null;
}
} |
<filter>
<filter-name>filter1</filter-name>
<filter-class>com.zj.sample.SimpleFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/*</url-pattern>//为所有的访问做过滤
</filter-mapping>
<filter>
<filter-name>filter2</filter-name>
<filter-class>com.zj.sample.SimpleFilter2</filter-class>
</filter>
<filter-mapping>
<filter-name>filter2</filter-name>
<url-pattern>/*</url-pattern>//为所有的访问做过滤
</filter-mapping> |
package
com.zj.sample;
import
java.io.IOException;
import
java.util.Date;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.http.HttpServletRequest;
public
class SimpleFilter1 implements Filter
{
@SuppressWarnings("unused")
private FilterConfig filterConfig;
public void init(FilterConfig config)
throws ServletException {
this.filterConfig = config;
}
public void doFilter(ServletRequest
request, ServletResponse response,
FilterChain chain) {
try {
System.out.println("Within SimpleFilter1:Filtering the
Request...");
HttpServletRequest req = (HttpServletRequest) request;
System.out.println(req.getRemoteHost() + " tried to access
"
+ req.getRequestURL() + " on " + new Date() +
".");
chain.doFilter(request, response);
System.out.println("Within SimpleFilter1:Filtering the
Response...");
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (ServletException se) {
se.printStackTrace();
}
}
public void destroy() {
this.filterConfig = null;
}
} |
package
com.zj.sample;
import
java.io.IOException;
import
java.text.DateFormat;
import
java.util.Calendar;
import
java.util.GregorianCalendar;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletContext;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.http.HttpServletRequest;
public
class SimpleFilter2 implements Filter
{
@SuppressWarnings("unused")
private FilterConfig config;
private ServletContext context;
private int startTime,
endTime;
private DateFormat formatter;
public void init(FilterConfig config)
throws ServletException {
this.config = config;
context =
config.getServletContext();
formatter =
DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM);
try {
startTime =
Integer.parseInt(config.getInitParameter("startTime"));//
web.xml
endTime = Integer.parseInt(config.getInitParameter("endTime"));//
web.xml
}
catch (NumberFormatException nfe) { // Malformed or
null
// Default: access at or after 10 p.m. but before 6 a.m. is
// considered unusual.
startTime = 22; // 10:00 p.m.
endTime = 6; // 6:00 a.m.
}
}
public void doFilter(ServletRequest
request, ServletResponse response,
FilterChain chain) {
try {
System.out.println("Within SimpleFilter2:Filtering the
Request...");
HttpServletRequest req = (HttpServletRequest) request;
GregorianCalendar calendar = new
GregorianCalendar();
int currentTime =
calendar.get(Calendar.HOUR_OF_DAY);
if (isUnusualTime(currentTime, startTime, endTime))
{
context.log("WARNING: " + req.getRemoteHost() + " accessed "
+ req.getRequestURL() + " on "
+ formatter.format(calendar.getTime()));
// The log file is under <CATALINA_HOME>/logs.One log per
day.
}
chain.doFilter(request, response);
System.out
.println("Within SimpleFilter2:Filtering the Response...");
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (ServletException se) {
se.printStackTrace();
}
}
public void destroy() {}
// Is
the current time between the start and end
//
times that are marked as abnormal access times?
private boolean
isUnusualTime(int currentTime, int
startTime, int endTime) {
// If the
start time is less than the end time (i.e.,
// they are
two times on the same day), then the
// current
time is considered unusual if it is
// between
the start and end times.
if (startTime < endTime) {
return ((currentTime >= startTime) &&
(currentTime < endTime));
}
// If the
start time is greater than or equal to the
// end time
(i.e., the start time is on one day and
// the end
time is on the next day), then the current
// time is
considered unusual if it is NOT between
// the end
and start times.
else {
return (!isUnusualTime(currentTime, endTime,
startTime));
}
}
} |
<Logger
className="org.apache.catalina.logger.FileLogger" prefix="catalina_log."
suffix=".txt"
timestamp="true"/> |
Servlet过滤器介绍之原理分析,布布扣,bubuko.com
原文:http://www.cnblogs.com/564085446java/p/3725270.html