Java Reference
In-Depth Information
In fact, it is possible to configure a chain of filters to be executed for each request, as
shown in Figure 3-12.
Figure 3-12. Servlet filter chain
Filters are pluggable components that provide preprocessing and postprocessing
support around a servlet. This technique works with JSPs too, because they are effectively
servlets. The filters are configured in web.xml without affecting the existing application
code. Listing 3-42 shows the servlet filter used to add time-based access to the produc-
tion application.
Listing 3-42. TimebasedAccessFilter.java
public class TimebasedAccessFilter implements Filter {
private int startHour;
private int endHour;
public void destroy() {}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
int currentHrofDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if((startHour <= currentHrofDay) && (currentHrofDay <= endHour)){
chain.doFilter(request, response);
}
else{
Search WWH ::




Custom Search