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…
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.
Here’s how you can create a Niagara Module that extends a standard javax.servlet.http.HttpServlet
class…
javax.servlet.http.HttpServlet
.doGet
or any other HTTP verb related methods to handle implementation.getUserPrincipal()
: cast this to a BUser.getLocale()
: returns a java.util.Locale object. Use the forLanguageTag and toLanguageTag methods to get the Locale to and from a String. Please note that javax.baja.util.Lexicon has a make method that takes a Locale object. Use a combination of these two methods to create an instance of javax.baja.sys.BasicContext. Please note, NCCB-7051 will address some of the issues concerning country and variant.WEB-INF
.web.xml
.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>
*
in the pattern means that anything after /test/… will also be picked up by this Servlet. This is very useful if you’re making a RESTful API. The XML listed here is all part of the J2EE standard. There’s nothing specific to the Jetty implementation.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
HttpServletRequest#getRequestURI()
(as is typically done in AX). Servlets should be relative and reusable. Most of the time they really don’t need to know about their precise plug point within a Web Server!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>
contextPath
has been added to the file.http://localhost/somethingelse/test/whatever
.As well as Servlets, developers can now use javax.servlet.Filter
.
Filter
interface can intercept HTTP Requests before they get to their Servlet
.Click here for more information on using Java Filters.
This isn’t currently supported in Niagara 4.0.
Copyright © 2000-2019 Tridium Inc. All rights reserved.