User Tools

Site Tools


technology:java:visual_calendar_servlet

Visual Calendar Servlet

This servlet was intended to render an HTML calendar on the screen. Good exercise in learning how to handle date math visually (i.e. what column number does the first date of a month fall on, and how many rows are in the current month?) I always intended to extend this servlet so it could retrieve content from a database and place it accordingly, but I never finished that code.

Written approximately January 2002, this was a *very* early attempt at web-based calendaring. It has been supplanted by a hundred other tools now, but still provides a good foundation in understanding visual math.

This servlet depends on my datetools class.

/**
 * Insert the type's description here.
 * Creation date: (9/26/01 10:20:12 AM)
 * @author: Administrator
 */
 
import java.io.*;
import java.util.*;
import DateTools;
public class CFWebCalendar extends javax.servlet.http.HttpServlet {
/**
 * Process incoming HTTP GET requests
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
 
    performTask(request, response);
 
}
/**
 * Process incoming HTTP POST requests
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
 
    performTask(request, response);
 
}
/**
 * Returns the servlet info string.
 */
public String getServletInfo() {
 
    return super.getServletInfo();
 
}
/**
 * Initializes the servlet.
 */
public void init() {
    // insert code to initialize the servlet here
 
}
/**
 * Insert the method's description here.
 * Creation date: (9/27/01 11:49:25 AM)
 * @return java.lang.String
 * @param month int
 * @param year int
 */
public String nextYearURL(int month, int year) {
    return "<A HREF=\"http://www.thefreyers.net/servlets/CFWebCalendar?month="
            + month
            + "&year="
            + (year+1)
            + "\">Next Year</A>";
}
/**
 * Process incoming requests for information
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */
public void performTask(
    javax.servlet.http.HttpServletRequest request,
    javax.servlet.http.HttpServletResponse response) {
 
    try {
        PrintWriter res = response.getWriter();
        res.println("<HTML><HEAD></HEAD><BODY>");
 
        //check parameters (if bad or null, set to current month)
        String mo = request.getParameter("month");
        String yr = request.getParameter("year");
        int month = (mo == null) ? 0 : Integer.parseInt(mo);
        int year = (yr == null) ? 0 : Integer.parseInt(yr);
 
        if ((month<1) || (month>12) ||(year<1970)||(year>3000)) {
            Calendar cal = new GregorianCalendar();
            month = cal.get(Calendar.MONTH) + 1; //"+1" for 0-based months
            year = cal.get(Calendar.YEAR);
        }
        renderCalendar(month, year, response);
        res.println("</BODY></HTML>");
    } catch (Throwable theException) {
        // uncomment the following line when unexpected exceptions
        // are occuring to aid in debugging the problem.
        theException.printStackTrace();
    }
 
}
/**
 * Insert the method's description here.
 * Creation date: (9/27/01 11:45:10 AM)
 * @return java.lang.String
 * @param month int
 * @param year int
 */
public String previousYearURL(int month, int year) {
    return  "<A HREF=\"http://www.thefreyers.net/servlets/CFWebCalendar?month="
            + month
            + "&year="
            + (year-1)
            + "\">Previous Year</A>";
}
/**
 * Insert the method's description here.
 * Creation date: (9/27/01 9:07:51 AM)
 * @param month int (values 1-12)
 * @param year int (values 1582 or larger int)
 * @param req javax.servlet.http.HttpServletRequest
 * @param resp javax.servlet.http.HttpServletResponse
 */
public void renderCalendar(int month, int year, javax.servlet.http.HttpServletResponse resp)
{
    try {
        PrintWriter res = resp.getWriter();
        int userMonth = month-1;  //java months are 0-based
        int userYear = year;
 
        //setup  parameters to start & end the dates while we're looping
        int firstOfMonth = DateTools.CalcFirstOfMonth(userYear, userMonth);
        int endOfMonth = DateTools.CalcEndOfMonth(userYear, userMonth);
        int numRows = DateTools.NumberRowsNeeded(userYear, userMonth);
 
        //output the header & navigation
        res.println("<TABLE BORDER=1 WIDTH=100%>");
        res.println(
            "<TR><TD colspan=7>"
                + "<TABLE BORDER=0 CELLPADDING=4 CELLSPACING=0 BGCOLOR=#CCCCCC WIDTH=100%>"
                + "<TR><TD ALIGN=CENTER>"
                + "<H1>Calendar for "
                + DateTools.months[userMonth]
                + " "
                + userYear
                + "</H1>"
                + previousYearURL(month, year)
                + "&nbsp;&nbsp;&nbsp;&nbsp;"
                + urlsForYear(year)
                + "&nbsp;&nbsp;&nbsp;&nbsp;"
                + nextYearURL(month, year)
                + "</TD></TR></TABLE>"
                + "</TD></TR>");
 
        //output the row of day names
        for (int d = 0; d < 7; d++) {
            res.println("<TD WIDTH=14.25% ALIGN='center' BGCOLOR='9999CC'>");
            res.println(DateTools.days[d]);
            res.println("</TD>");
        }
        res.println("</TR>");
 
        //nested loop to produce calendar
        int position = 0;
        int thisDate = 0;
        for (int row = 0; row < numRows; row++) {
            res.println("<TR>");
            for (int col = 0; col < 7; col++) {
                position = (row * 7) + col;
                thisDate = position - firstOfMonth + 1;
 
                //print empty cells before and after valid days of month
                if ((thisDate < 1) || (thisDate > endOfMonth)) {
                    res.println("<TD HEIGHT=100px VALIGN=TOP ALIGN=LEFT>&nbsp;</TD>");
                    continue;
                }
                //output info for this date on valid days of month
                RenderCell(userYear, userMonth, thisDate, resp);
            }
            res.println("</TR>");
        }
        res.println("</TABLE>");
 
        //------------------------------
    } catch (Throwable theException) {
        // uncomment the following line when unexpected exceptions
        // are occuring to aid in debugging the problem.
        theException.printStackTrace();
    }
 
    }
/**
 * Insert the method's description here.
 * Creation date: (9/26/01 12:43:02 PM)
 * @param year int
 * @param month int
 * @param date int
 */
public void RenderCell(
    int year,
    int month,
    int date,
    javax.servlet.http.HttpServletResponse response)
    throws java.io.IOException {
 
    PrintWriter res = response.getWriter();
    res.println("<TD HEIGHT=100px  VALIGN=TOP ALIGN=LEFT>");
    res.println("<B>"+date+"</B><BR>");
    res.println("</TD>");
 
}
/**
 * Insert the method's description here.
 * Creation date: (9/27/01 12:30:28 PM)
 * @return java.lang.String
 * @param year int
 */
public String urlsForYear(int year) {
    String months[] =
        {
            "Jan",
            "Feb",
            "Mar",
            "Apr",
            "May",
            "Jun",
            "Jul",
            "Aug",
            "Sept",
            "Oct",
            "Nov",
            "Dec" };
    String URL = "";
 
    for (int i = 1; i <= 12; i++) {
        URL
            += "<A HREF=\"http://www.thefreyers.net/servlets/CFWebCalendar?month="
            + i
            + "&year="
            + year
            + "\">"
            + months[i - 1]
        +"</A>"
        +((i<12)?"&nbsp;&nbsp;":"");
    }
    return URL;
}
}
/home/cfreyer/public_html/data/pages/technology/java/visual_calendar_servlet.txt · Last modified: 2009/04/01 10:37 by Chris Freyer