Filtering the Inexperienced

We demonstrate another technique for filtering the inexperienced (retirees with less than 10 years of service).

We begin by creating a service filter. Here's the doFilter method:

    public void doFilter(
      ServletRequest request,
      ServletResponse response,
      FilterChain chain) throws IOException, ServletException {
                        
        int years = new Integer(request.getParameter("years"));
       
        ServletContext application =
            _filterConfig.getServletContext();
        Integer minYears =
            (Integer)application.getAttribute("minYears");
        if (minYears == null) {
            minYears = new Integer(10);
            application.setAttribute("minYears", minYears);
        }
       
        if (years < minYears) {
          ((HttpServletResponse)response).sendRedirect(
               "serviceWarning.html");
          return;       
        }
        chain.doFilter(request, response);
    }

This time we assume the minimum number of years of service is stored in the servlet context, application. If it's not, then we put it there. We can also access the session object with the following code:

HttpSession session = ((HttpServletRequest)request).getSession(true);

Secondly, we redirect the response to a special service warning web page if theyears of service are insufficient.
To demonstrate, here's the request:

And the response: