> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/get-started/samples/webviewer-tomcat-java.md).

# Integrate WebViewer into Tomcat JavaServer Pages (JSP)

This sample adds WebViewer as a Web Application that is hosted on a Tomcat Servlet

Using an Apache Tomcat 9.0 Servlet/JSP container, WebViewer is defined as a web application to be hosted and displayed when navigated to. WebViewer is able to view, annotate, and manipulate PDFs and other documents.

The sample also demonstrates how to programmatically add a rectangle annotation with user-defined dimensions and includes a number of example files that can be loaded (PDF, XOD, and an annotation XFDF sample).

WebViewer provides a slick out-of-the-box responsive UI that enables you to view, annotate and manipulate PDFs and other document types inside any web project.

Click the button below to view the full project in GitHub.

{% tabs %}
{% tab title="JavaScript" %}
{% code title="index.js" lineNumbers="true" %}

```js
WebViewer({
    path: 'lib',
    initialDoc: 'files/webviewer-demo-annotated.pdf'
  }, document.getElementById('viewer'))
  .then(function(instance) {

    // you can access documentViewer object for low-level APIs
    const { documentViewer } = instance.Core;

    // now you can access APIs through this.webviewer.getInstance()
    instance.UI.openElements(['notesPanel']);
    // see https://www.pdftron.com/documentation/web/guides/ui/apis for the full list of APIs
    
    // or listen to events from the viewer element
    documentViewer.addEventListener('pageNumberUpdated', (pageNumber) => {
      console.log(`Current page is ${pageNumber}`);
    });

    // or from the documentViewer instance
    documentViewer.addEventListener('annotationsLoaded', () => {
      console.log('annotations loaded');
    });

    documentViewer.addEventListener('documentLoaded', function() {
      // and access classes defined in the WebViewer iframe
      const { Annotations, annotationManager } = instance.Core;
      const rectangle = new Annotations.RectangleAnnotation();
      rectangle.PageNumber = 1;
      rectangle.X = 100;
      rectangle.Y = 100;
      rectangle.Width = 250;
      rectangle.Height = 250;
      rectangle.StrokeThickness = 5;
      rectangle.Author = annotationManager.getCurrentUser();
      annotationManager.addAnnotation(rectangle);
      annotationManager.drawAnnotations(rectangle.PageNumber);
      // see https://www.pdftron.com/api/web/WebViewer.html for the full list of low-level APIs
    });

  });

```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/get-started/samples/webviewer-tomcat-java.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
