Create Java Servlet extending Quark API Servlet and define remote service address.
API discovery and Controller will be available through HTTP channel at http://localhost/[WEB_APP]/api
@WebServlet("/api")
public class DemoAPIServlet extends APIServlet {
}
Optionally, create Java WebSocket extending Quark API WebSocket Module and define remote service address.
WebSocket channel will be available at ws://localhost/[WEB_APP]/socket
@ExtJSSession(required = false)
@ServerEndpoint(
value = '/socket',
configurator = WebSocketConfigurator.class,
decoders = { WebsocketDecoder.class},
encoders = { WebsocketEncoder.class})
public class DemoAPIWebSocketService extends WebSocketService {
}
Finally, create Java Controller class which will be exposed to the web.
Generated web object and function will be io.greenscreens.Demo.hello
API discovery and Controller will be available at
HTTP channel http://localhost/[WEB_APP]/api
WebSocket channel ws://localhost/[WEB_APP]/socket
@ExtJSDirect(paths = { '/socket', '/api' })
@ExtJSAction(namespace = 'io.greenscreens', action = "Demo")
public class DemoController {
@ExtJSMethod("hello")
public ExtJSResponse helloWorld(final String name) {
ExtJSResponse resp = new ExtJSResponse(true);
resp.setMsg("Hello ".concat(name));
return resp;
}
}
Include only 7Kb small quark.min.js script into your page
<html>
<head>
<script type="text/javascript" charset="UTF-8" src="lib/quark.min.js"></script>
</head>
<body>
</body>
</html>
Prepare service URLs API for discovery and http channel, or optionally for WebSocket.
const ws = 'ws://localhost/demo/socket`;
const api = 'http://localhost/demo/api`;
Initialize engine. Engine will use provided URLs to register exposed Java classes and methods.
await Engine.init({api: api, service:api});
or
await Engine.init({api: api, service:ws});
Call Java exposed method. Namespace 'io.greenscreens' , action 'Demo' and method 'hello' are the same as defined Java Class on server side.
const data = await io.greenscreens.Demo.hello('John Doe');
console.log(data);