Restricting Access Based on Time of Day


You can also use filters to restrict access to Web pages based on time of day; for example, you can restrict access to a game, data that needs to be updated, and so on.

Here’s an example, time.jsp, which you can access if the time of day is right:

 <html>     <head>         <title>Using Filters to Restrict Access</title>     </head>     <body>         <h1>Using Filters to Restrict Access</h1>         Congratulations, you're in!         <br>     </body> </html>

To write the time-based filter TimeFilter.java, you start like this, declaring the TimeFilter class:

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;  public final class TimeFilter implements Filter {   public void doFilter(ServletRequest request, ServletResponse     response,     FilterChain chain)     throws IOException, ServletException   {         .         .         .   }   public void destroy()   {   }   public void init(FilterConfig filterConfig)   {   } }

In the doFilter method, you need to start by getting the current time. Say that you want to restrict access to time.jsp such that it wasn’t available between the hours of 9 to 5. You start that process in Java by creating a GregorianCalendar object and a Date object:

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public final class TimeFilter implements Filter {   public void doFilter(ServletRequest request, ServletResponse     response,     FilterChain chain)     throws IOException, ServletException   {     GregorianCalendar calendar = new GregorianCalendar();     Date date1 = new Date();         .         .         .   }   public void destroy()   {   }   public void init(FilterConfig filterConfig)   {   } }

Then you set the time in the GregorianCalendar object with the setTime method, passing the Data object to that method, and get the current hour of the day with the GregorianCalendar object’s get method:

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public final class TimeFilter implements Filter  {   public void doFilter(ServletRequest request, ServletResponse      response,     FilterChain chain)     throws IOException, ServletException    {     GregorianCalendar calendar = new GregorianCalendar();     Date date1 = new Date();     calendar.setTime(date1);     int hour = calendar.get(Calendar.HOUR_OF_DAY);         .         .         .   }   public void destroy()    {    }   public void init(FilterConfig filterConfig)    {   } }

If the hour is not between 9 and 5, you can call the next link in the chain, which is time.jsp:

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public final class TimeFilter implements Filter  {   public void doFilter(ServletRequest request, ServletResponse      response,     FilterChain chain)     throws IOException, ServletException    {     GregorianCalendar calendar = new GregorianCalendar();     Date date1 = new Date();     calendar.setTime(date1);     int hour = calendar.get(Calendar.HOUR_OF_DAY);     if(hour < 9 || hour > 17) {            chain.doFilter(request, response);     }          .         .         .     }     public void destroy()      {      }     public void init(FilterConfig filterConfig)      {     }   }

Otherwise, you want to restrict access to time.jsp, which you can do by sending a nasty page back to the browser, like this:

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public final class TimeFilter implements Filter  {   public void doFilter(ServletRequest request, ServletResponse      response,     FilterChain chain)     throws IOException, ServletException    {     GregorianCalendar calendar = new GregorianCalendar();     Date date1 = new Date();     calendar.setTime(date1);     int hour = calendar.get(Calendar.HOUR_OF_DAY);     if(hour < 9 || hour > 17) {            chain.doFilter(request, response);     }     else {         response.setContentType("text/html");         PrintWriter out = response.getWriter();         out.println("<html>");         out.println("<head>");         out.println("<title>");         out.println("Get Back to Work!");         out.println("</title>");         out.println("</head>");         out.println("<body>");         out.println("<H1>Get Back to Work!</H1>");         out.println("Sorry, that resource is not available            now.");         out.println("</body>");         out.println("</html>");     }       }   public void destroy()    {    }   public void init(FilterConfig filterConfig)    {   } } 

That completes TimeFilter.java. After compiling it, place it in the application’s WEB-INF\classes directory:

 webapps   |   |__chap16        |        |__WEB-INF            |  |            |  |__web.xml            |            |__classes            |  |            |  |__TimeFilter.class            |            |__lib

You also need to install TimeFilter in web.xml, as a filter for the time.jsp page, and you do that like this:

 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application        2.3//EN"     "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app>   <filter>     <filter-name>Log Filter</filter-name>     <filter-class>LogFilter</filter-class>   </filter>   <filter>     <filter-name>Authentication</filter-name>     <filter-class>PasswordFilter</filter-class>   </filter>   <filter>     <filter-name>Time</filter-name>     <filter-class>TimeFilter</filter-class>   </filter>   <filter-mapping>     <filter-name>Log Filter</filter-name>     <url-pattern>/log.jsp</url-pattern>   </filter-mapping>   <filter-mapping>     <filter-name>Authentication</filter-name>     <url-pattern>/password.jsp</url-pattern>   </filter-mapping>   <filter-mapping>     <filter-name>Time</filter-name>     <url-pattern>/time.jsp</url-pattern>   </filter-mapping> </web-app>

You can see the results when you navigate to time.jsp during the middle of the day, as shown in Figure 16.6, where the filter is blocking access.

image from book
Figure 16.6: The Time filter at work



Ajax Bible
Ajax Bible
ISBN: 0470102632
EAN: 2147483647
Year: 2004
Pages: 169

Similar book on Amazon
HTML, XHTML, and CSS Bible
HTML, XHTML, and CSS Bible
JavaScript Bible
JavaScript Bible
JavaScript Bible
JavaScript Bible
PHP and MySQL Web Development (4th Edition)
PHP and MySQL Web Development (4th Edition)

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net