Restricting Access Based on Time of Day


The list of things you can do with filters is endless. You can convert, for example, the data sent to a servlet from XML to HTML, or even from French to English. You can add a common header or footer to all web pages in your site. You can use a single filter as a gateway to your entire site. You can restrict access to a JSP or servlet in case multithreading issues become a problem (on most web servers, multiple threads from multiple users can execute the same servlet or JSP code at the same time).

You can even restrict access to a resource based on the time of day. Say you have a company full of employees and don't want them to access a game on your site during the day. Here's what the restricted starting page of the game, game.jsp, might look like:

 <HTML>     <HEAD>         <TITLE>Welcome to the time-wasting game</TITLE>     </HEAD>     <BODY>         <H1>Welcome to the time-wasting game</H1>         Congratulations, you're in!         <BR>     </BODY> </HTML> 

Slapping a filter on game.jsp will make it inaccessible during working hours, as you can see in Figure 6.3.

Figure 6.3. Restricting access based on time of day with a filter.


Writing a filter like this one isn't difficult. This one will be called Restrict.java, and it starts with the three Filter interface methods you need to implement, init, destroy, and doFilter:

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public final class Restrict 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 can use the Java GregorianCalendar class to get an object that can act as a calendar, and a Date object to get the current time. If the current time is outside working hourssay 9 to 5you can pass on access to the filtered resource, game.jsp, simply by calling the FilterChain object's doFilter method:

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public final class Restrict 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, if it's during working hours, you can display the error message you see in Figure 6.3 by returning this HTML yourself:

 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public final class Restrict 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("The game is not available");         out.println("</TITLE>");         out.println("</HEAD>");         out.println("<BODY>");         out.println("<H1>The game is not available</H1>");         out.println("Sorry, that resource may not be accessed now.");         out.println("</BODY>");         out.println("</HTML>");     }   }   public void destroy()   {   }   public void init(FilterConfig filterConfig)   {   } } 

That's all you need for Restrict.java. Store game.jsp in the logger directory, and after compiling Restrict.java, store Restrict.class in the logger classes directory.

As before, you have to connect this new filter to the resource it filters in logger's web.xml file. Start by connecting the filter (named, say, "Restricting Filter") to its Java class in a <filter> element:

 <?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>Simple Filter</filter-name>     <filter-class>Simple</filter-class>   </filter>   <filter>     <filter-name>Restricting Filter</filter-name>     <filter-class>Restrict</filter-class>   </filter>   <filter-mapping>     <filter-name>Simple Filter</filter-name>     <url-pattern>/simple.jsp</url-pattern>   </filter-mapping> </web-app> 

Then connect this new filter to game.jsp in a <filter-mapping> element, like this, in web.xml:

 <?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>Simple Filter</filter-name>     <filter-class>Simple</filter-class>   </filter>   <filter>     <filter-name>Restricting Filter</filter-name>     <filter-class>Restrict</filter-class>   </filter>   <filter-mapping>     <filter-name>Simple Filter</filter-name>     <url-pattern>/simple.jsp</url-pattern>   </filter-mapping>   <filter-mapping>     <filter-name>Restricting Filter</filter-name>     <url-pattern>/game.jsp</url-pattern>   </filter-mapping> </web-app> 

That's all you need. To see this one at work, navigate to http://localhost:8080/logger/game.jsp.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

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