# KeyMaster Servlets

KeyMaster includes two example servlets.

## Token Generator Servlet

The KeyMaster Java kit includes the StandardKeyMasterServlet token generator example servlet.

This servlet can be configured via servlet parameters in the associated `web.xml`. It can also be used as the basis for your own custom token generator servlet.

### Example web.xml config for StandardKeyMasterServlet

```xml
<servlet>
  <servlet-name>StandardKeyMaster</servlet-name>
  <servlet-class>com.caplin.keymaster.servlet.StandardKeyMasterServlet</servlet-class>

  <init-param>
    <param-name>caplin.keymaster.privatekey.filename</param-name>
    <param-value>private.pem</param-value>
    <description>Name of the private key filename</description>
  </init-param>
</servlet>
```

The following servlet parameters can be specified in the web.xml:

| Parameter | Type | Description |
| --- | --- | --- |
| caplin.keymaster.privatekey.filename | Required | The location of the PEM PKCS#8 formatted private key file relative to the webapp. |
| caplin.keymaster.hashing.algorithm | Optional | Hashing algorithm name. See the [com.caplin.keymaster.KeyMasterHashingAlgorithm,opts="nofollow"](https://docs.caplin.com/developer/api/keymaster/6.2/com/caplin/keymaster/KeyMasterHashingAlgorithm.html) enumeration for valid algorithm names. The default algorithm is SHA256. |
| caplin.keymaster.security.provider.class.name | Optional | Class name of a security provider to add to those available. |
| caplin.keymaster.security.provider.name | Optional | Name of security provider to use for the token generation. |

## Poll Servlet

The KeyMaster Java kit includes a Poll servlet.

A typical Java KeyMaster deployment uses a Poll servlet which defines the interface that enables the application to receive KeyMaster keep-alive responses.

It is configured as shown below.

```xml
<servlet>
  <servlet-name>Poll</servlet-name>
  <servlet-class>com.caplin.keymaster.servlet.Poll</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Poll</servlet-name>
  <url-pattern>/servlet/Poll</url-pattern>
</servlet-mapping>
```

### StreamLink Configuration

StreamLink accesses Poll at regular intervals to keep the KeyMaster servlet session alive.

The StreamLink configuration is as follows:

```js
var streamlink = caplin.streamlink.StreamLinkFactory.create({
        liberator_urls: ["rttps://primary-liberator.example.com", "rttps://secondary-liberator.example.com"],
        keymaster_url: "servlet/StandardKeyMaster",
        keymaster_keep_alive_url: "servlet/Poll"
});
```

### Receiving Callbacks

To receive callbacks, add `KeymasterPollResponseListener` to `caplin.streamlink.StreamLinkFactory.create` method. You will be notified when `onKeymasterError` is called and asked to sign in again.

**Example**

```js
caplin.streamlink.StreamLinkFactory.create({
  liberator_urls: [
    "rttps://primary-liberator.example.com",
    "rttps://secondary-liberator.example.com"
  ],
  keymaster_url: "servlet/StandardKeyMaster",
  keymaster_keep_alive_url: "servlet/Poll",
  keymaster_keep_alive_listener: {
    onKeymasterError: function(reason) {
      console.log("KeyMaster Failed due to : " + reason);
    },
    onKeymasterResponse : function(response) {
      console.log("KeyMaster Success. Response Message : " + response);
    }
  }
});
```

More details on how to configure StreamLink using KeyMaster are [here](../streamlink/index.md).

---

**See also:**

* [KeyMaster Concepts](keymaster-keymaster-concepts.md)
* [KeyMaster Java API,opts="nofollow"](https://docs.caplin.com/developer/api/keymaster/6.2/#overview_description)
* [KeyMaster .NET API Configuration](keymaster-keymaster-.net-api-configuration.md)
