MyServlet.java Source code

Java tutorial

Introduction

Here is the source code for MyServlet.java

Source

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.security.*;

public class MyServlet 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) {
    }
}