Monthly Event Calendar for Java Tutorial


We will assume that your HTML file (either .html or .jsp) is located in Month 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/month.js"></script>

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

<div id="dpm">

Add the JavaScript initialization code:

<script type="text/javascript">
var dpm = new DayPilot.Month("dpm");
dpm.backendUrl = "${pageContext.request.contextPath}/dpm";

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

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

2. Libraries

JavaScript libraries

  • Copy common.js and month.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 "/dpm" 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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.daypilot.demo.db.Db;
import org.daypilot.ui.DayPilotMonth;
import org.daypilot.ui.args.month.EventMenuClickArgs;
import org.daypilot.ui.args.month.BeforeCellRenderArgs;
import org.daypilot.ui.args.month.BeforeEventRenderArgs;
import org.daypilot.ui.args.month.CommandArgs;
import org.daypilot.ui.args.month.EventMoveArgs;
import org.daypilot.ui.args.month.EventResizeArgs;
import org.daypilot.ui.args.month.TimeRangeSelectedArgs;
import org.daypilot.ui.enums.TimeFormat;
import org.daypilot.ui.enums.UpdateType;
import org.daypilot.ui.enums.WeekStarts;
import org.daypilot.util.LocaleParser;

public class DpmServlet 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 {


    Dpm dpm = new Dpm();
    dpm.process(request, response);


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

4. Dpm Class

Create a new Dpm class (it must inherit from org.daypilot.ui.DayPilotMonth):

public class Dpm extends DayPilotMonth {

  public void onInit() throws Exception {

    // map the database column names

    // assign the collection of events
    setEvents(Db.getEvents(getRequest(), getVisibleStart(), getVisibleEnd()));

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

The onInit() method is called using an AJAX callback right after the Month 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(), null);

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

dpm.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(), getVisibleStart(), getVisibleEnd()));


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(), null);

Both events need to be enabled on the client side:

dpm.eventResizeHandling = "CallBack";
dpm.timeRangeSelectedHandling = "CallBack";

DayPilot for JavaScript, ASP.NET WebForms, ASP.NET MVC, Java