Niagara Web Modules

In Niagara 4 standard Java Web Server technology can be used.

The Web Server currently being used is Jetty. Jetty is built around the standard Java Servlet Specification.

Currently, the Jetty supports version 3 of the Java Servlet Specification. Currently we’re not supporting the newer Servlet annotations; a Web XML descriptor must be used instead.

In Niagara AX, there are two other ways of creating Java Servlets. These are still supported in Niagara 4…

Niagara 4

Please note that applications using BWebServlet and BServletView are still and will continue to be supported. From Niagara 4, we’ve additionally added support for adding standard Java Servlets that extend javax.servlet.http.HttpServlet.

Please click here for more information on using Java Servlets.

My First Niagara Web Module

Here’s how you can create a Niagara Module that extends a standard javax.servlet.http.HttpServlet class…

jar {
  from('src') {
    ...
    include 'WEB-INF/*.xml'
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>mypackage.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/test/*</url-pattern>
  </servlet-mapping>
</web-app>

Now try building your module and starting up a Station. On Station start up, the Servlets will be automatically installed into the Web Server. No more configuration is required. Once the Station has started, the URL to access the Servlet would be in the following format…

http://localhost/moduleName/test

Or as we’re also using a * in the URI pattern…

http://localhost/moduleName/test/whatever/foobar

Changing the Context Path

To change the Context Path of the Servlet we need to add one more XML file alongside web.xml called jetty-web.xml. This file configures functionality specific to the Jetty Web Server that can’t be done via web.xml. By default, using the Niagara Module name is used for Context Path. This is a sound way to try and create a unique path mapping for a web application. This can be changed with the following jetty-web.xml file…

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="webApp" class="org.eclipse.jetty.webapp.WebAppContext">
  <!-- Change the Context Path from the module name to something else -->
  <Set name="contextPath">/somethingelse</Set>
</Configure>

Filters

As well as Servlets, developers can now use javax.servlet.Filter.

Click here for more information on using Java Filters.

How can I use a proper WAR file?

This isn’t currently supported in Niagara 4.0.