Event Calendar for Java Tutorial


We will assume that your HTML file (either .html or .jsp) is located in Calendar directory of your web application. If you use a different location you must change the relative URLs accordingly.

Add the references to the required JavaScript libraries to the header or to the body:

<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/calendar.js"></script>

Put a placeholder for the Calendar into the <body> :

<div id="dpc">

Add the JavaScript initialization code:

<script type="text/javascript">
var dpc = new DayPilot.Calendar("dpc");
dpc.backendUrl = '${pageContext.request.contextPath}/dps';
dpc.viewType = "Week";

It is necessary to specify the backendUrl property. That's the URL which handles the AJAX callback requests.

The complete API reference for the event calendar client-side object can be found here:

2. Libraries

JavaScript libraries

  • Copy common.js and calendar.js to js directory of your web application.

Java libraries

  • Copy daypilot-x.x.x.jar (e.g. daypilot-1.0.30.jar) to WEB-INF/lib directory of your web application.

3. Servlet

Create a new servlet and map it to "/dpc" Url. You can do it by adding the following declaration to web.xml file:

  <description /> 

The servlet class will be very simple:

package org.daypilot.demo;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.daypilot.data.Column;
import org.daypilot.date.DateTime;
import org.daypilot.date.Week;
import org.daypilot.demo.db.Db;
import org.daypilot.ui.DayPilotCalendar;
import org.daypilot.ui.args.calendar.BeforeCellRenderArgs;
import org.daypilot.ui.args.calendar.BeforeEventRenderArgs;
import org.daypilot.ui.args.calendar.BeforeHeaderRenderArgs;
import org.daypilot.ui.args.calendar.CommandArgs;
import org.daypilot.ui.args.calendar.EventBubbleArgs;
import org.daypilot.ui.args.calendar.EventMenuClickArgs;
import org.daypilot.ui.args.calendar.EventMoveArgs;
import org.daypilot.ui.args.calendar.EventResizeArgs;
import org.daypilot.ui.args.calendar.TimeRangeSelectedArgs;
import org.daypilot.ui.enums.UpdateType;
import org.daypilot.ui.enums.ViewType;

public class DpcServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    DayPilotCalendar dpc = new Dpc();
    dpc.process(request, response);

This will forward the POST request to a special Dpc class which we will create in the next step.

4. Dpc Class

Create a new Dpc class (it must inherit from org.daypilot.ui.DayPilotCalendar):

public class Dpc extends DayPilotCalendar {

  public void onInit() throws Exception {

    // map the database column names

    // assign the collection of events
    setEvents(Db.getEvents(getRequest(), getStartDate().toDate(), getStartDate().addDays(getDays()).toDate()));

    // request an update of the event set on the client side

The onInit() method is called using an AJAX callback right after the Calendar is initialized on the client side.

5. Event Moving

In order to handle event move action, we need to override onEventMove() method. The event arguments are available in EventMoveArgs class (ea parameter).

public void onEventMove(EventMoveArgs ea) throws Exception {
  // update the DB
  Db.moveEvent(getRequest(), ea.getValue(), ea.getNewStart().toTimeStamp(), ea.getNewEnd().toTimeStamp(), ea.getNewResource());

It has to be enabled on the client-side as well:

dpc.eventMoveHandling = "CallBack"; 

6. Prepare and Finish

There are two special methods available for overriding:

  • onPrepare()
  • onFinish()

They are called during every AJAX callback request (before and after the main event method).

We will use onPrepare() to initialize an in-memory instance of a HSQLDB embedded database (which we will use for testing purposes):

public void onPrepare() throws Exception {
  // create the in-memory DB if it's not ready
  if (!Db.tableExists("EVENTS")) {

We will also move the event loading code there to the onFinish() method:

public void onFinish() throws Exception {

  if (getUpdateType() == UpdateType.NONE) {

  // set the database fields

  // reload events
  setEvents(Db.getEvents(getRequest(), getStartDate().toDate(), getStartDate().addDays(getDays()).toDate()));


7. Handling Other Events

Implementing event handlers is very similar to onEventMove(). Just add the required database changes. Since we already added onFinish() method, you don't need to care about refreshing the events.

Our new onEventResize handler:

public void onEventResize(EventResizeArgs ea) throws Exception {
  Db.resizeEvent(getRequest(), ea.getValue(), ea.getNewStart().toTimeStamp(), ea.getNewEnd().toTimeStamp());

And onTimeRangeSelected handler:

public void onTimeRangeSelected(TimeRangeSelectedArgs ea) throws Exception {
  Db.insertEvent(getRequest(), "New event", ea.getStart().toTimeStamp(), ea.getEnd().toTimeStamp(), ea.getResource());

Both events need to be enabled on the client side:

dpc.eventResizeHandling = "CallBack";
dpc.timeRangeSelectedHandling = "CallBack";