BBjServlet Overview

 

In BBj 12.0 and higher, BBjServlet allows programs to respond to web requests such as events. For additional reading about BBjServlets, read these articles from The BASIS International Advantage:

To respond to a web request, follow these three steps:

  1. Create an application with BBjAppConfig

  2. Obtain the servlet registry from BBjAdmin

  3. Publish the BBjAppConfig with the BBjServletRegistry

Create

BBj applications can require a lot of configuration. config files, working directories, program names, just to get started. The BBjAppConfig object encapsulates environment information and stores it so the program can be run in an automatic way, much like an autorun program, or BBj task.

Obtain

The BBjServletRegistry manages all currently running servlets. The BBjServletRegistry is used to publish BBjAppConfigs at specific paths. It is obtained from BBjAdmin, so administrative rights are required to add and remove servlets from the webserver.

Publish

With both the BBjServletRegistry and your BBjAppConfig, you can publish a new servlet to the webserver. Select a path (starting with “/servlet/”) and publish your application. New interpreters will be started and pooled depending on load. When the load declines, the interpreters will be shut down. Licenses are checked out only during request handling.


declare BBjAdmin admin!

declare BBjServletRegistry registry!

declare BBjConfig config!

declare BBjAppConfig application!

 

REM create

config! = BBjAPI().getConfig()

application! = config!.makeEmptyAppConfig()

application!.setProgramName("servlet.bbj")

 

REM obtain

admin! = BBjAPI().getAdmin("admin", "admin123")

registry! = admin!.getServletRegistry()

 

REM publish

registry!.unpublish("/example", err=*next)

registry!.publish("/example", application!)


What can servlets do?

Servlets can respond to all types of web requests such as GET and  POST. In order to respond to a web request, your servlet must register for the ON_WEB_CONNECTION event. That event provides lots of information about the request, such as the request method, query parameters and http headers.

When you’re ready to respond, print to the new JSERVLET channel, the output will be redirected back to the browser with whatever configuration options you wish to set on the BBjHttpResponse, such as contentType.


Example

data! = BBjAPI().getServletData()

MyServlet! = new MyServlet()

data!.setCallback(data!.ON_WEB_CONNECTION, myServlet!, "myMethod")

PROCESS_EVENTS

class public MyServlet
   field private BBjInt chan%
   method public MyServlet()
       #chan% = unt
   methodend
   method public void myMethod(BBjServletEvent p_event!)
       req! = p_event!.getHttpRequest()
       resp! = p_event!.getHttpResponse()
       resp!.setContentType("text/html")
       open(#chan%)"JSERVLET"
       print(#chan%)"<html><body><ul>"
       #listItem("method", req!.getMethod())
       #parameterList(req!)
       #headerList(req!)
       print(#chan%)"</ul>"
       close(#chan%)
   methodend

   method public void listItem(BBjString p_title$, BBjString p_item$)
       print(#chan%)"<li>"
       print(#chan%)p_title$, ":",p_item$
       print(#chan%)"</li>"
   methodend

   method public void parameterList(BBjHttpRequest req!)
       names! = req!.getParameterNames()
       print(#chan%)"<ul>"
       if names!.size() > 0
           for i = 0 to names!.size() - 1
               rem alternatively, req!.getParameterValues(param$)
               rem for multi valued params
               #listItem(names!.get(i), req!.getParameter(names!.get(i)))
           next i
       endif
       print(#chan%)"</ul>"
   methodend

   method public void headerList(BBjHttpRequest req!)
       names! = req!.getHeaderNames()
       print(#chan%)"<ul>"
       if names!.size() > 0
           for i = 0 to names!.size() - 1
               rem alternatively, req!.getHeaders(param$)
               rem for multi valued headers
               #listItem(names!.get(i), req!.getHeader(names!.get(i)))
           next i
       endif
       print(#chan%)"</ul>"
   methodend

classend

 

See Also

BBjServletEvent

BBjHttpRequest

      BBjFileUpload

BBjHttpSession

BBjHttpResponse

BBjCookie

BBjServletRegistry

BBjApplication

 

The BASIS International Advantage



______________________________________________________________________________________

Copyright BASIS International Ltd. BBj®, Visual PRO/5®, PRO/5®, and BBx® are registered trademarks.