BBxServlet Tutorial

This guide will take you through the process of creating and deploying a BBxServlet.

Creating a New BBxServlet

To create a new BBxServlet, you must:

  • Create a BBj source file for the servlet

  • Register the BBxServlet through one of the following:

    • The Enterprise Manager

    • The BBjAPI

Creating the BBxServlet Source File

For reference, see both the simple and complex servlets in the Example Servlets section below. You can copy either example into a BBj source code file, or create your own from scratch.

The BBj source code file must meet the following requirements:

  • It must have a custom class to represent your implementation of the servlet

  • The custom class must have a custom method to service the servlet

    • The method can be named anything you like, although both examples use the name "service"

    • The method must return void

    • The method must take a BBxServletContext as its only argument


When writing the code for your servlet, remember that through the BBxServletContext object, you have access to BBxServletRequest, BBxServletResponse, BBxWebSession, BBjJettyContext, and the BBjAPI.

Migrating a BBjspServlet

If you are migrating a BBjspServlet to a BBxServlet, you will find that the overall structure of your code will carry over without much modification. The most important thing to do is change the object and method names to their BBxServlet equivalents. See the table below for a list of conversions:

BBjspServlet BBxServlet
BBjspServletContext BBxServletContext
BBjspWebRequest BBxServletRequest
BBjspWebResponse BBxServletResponse
BBjspWebSession BBxWebSession

Registering the Servlet

In order for your BBxServlet to be accessible, you must register it with a context. The context you register your servlet with will determine the URL mapping that you use to access your servlet. To learn more about contexts, see the Advantage Article Don't Put All of Your Jetty Eggs in One Context.

BBxServlet Configuration Settings

When you register your servlet, you must specify the following:

  • The custom class name you used for your servlet in the source file

    • In the examples below, this is ExampleServlet or TestingServlet

  • The method name for the custom method that services your servlet

    • In the examples below, this is service

  • The absolute path to the source file of your servlet

  • A URL mapping that you will use to access the servlet

    • This may be anything you like, but must abide by standard URL formatting rules

    • You will access your servlet by appending the URL mapping to the URL of the context you register your servlet in

  • A status, indicating whether the servlet is enabled

You may also specify the following additional settings:

  • The absolute path to the config file for your servlet to use

    • If not specified, this setting will default to <bbjhome>/cfg/config.bbx

  • An SSCP for your servlet

    • The default SSCP is bbj_default

  • A Terminal Alias for your servlet

  • A Working Directory for your servlet

  • Additional initialization Parameters for your servlet (accessed via BBxServletContext::getInitParameter when the servlet is running).

Register with the Enterprise Manager

Registering your BBxServlet through the Enterprise Manager is the most user-friendly method.

  1. Go to the Web > Context Configuration section of the EM.

  2. Select the context you wish to add your servlet to.

  3. Scroll down to the BBx Servlets section.

  4. Click the Green Plus button to the right of the list of servlets to add a new BBxServlet

  5. In the Servlet Configuration window, fill in the required fields with the information from your servlet.

    See BBxServlet Configuration Settings above for information on the various configuration settings.

  6. Click OK when you have filled in all the desired settings for your servlet.

  7. Save the context by clicking the Save icon in the top left of the Context Configuration window.

  8. Restart the context by clicking the Restart button.

Register with the BBjAPI

Registering your BBxServlet through the BBjAPI is also very straightforward.

  1. Start BBjServices.

  2. Open up the BBj SysConsole.

  3. Use the following commands to register your servlet, replacing the text in angled brackets with the correct information from your servlet.

    Remember to use absolute file paths for the paths to the servlet file and config file.

    If you want your context path to be the root context (with just a "/" as the base path), put "root" in the CONTEXT_NAME field.

    .getAdmin("<USERNAME>", "<PASSWORD>")
  4. The addBBxServlet method does not encapsulate all of the fields available in the EM, but you are still able to set these fields with the following methods:

    BBxServlet::setClassPath(String sscp)

    BBxServlet::setTerminalAlias(String alias)

    BBxServlet::setWorkingDir(String dir)

    BBxServlet::addParameter(String name, String value)

  5. Restart the context with the following commands, again replacing the text in angled brackets with the specific information for your servlet:

    .getAdmin("<USERNAME>", "<PASSWORD>")

Accessing the Servlet

Once your servlet is registered, accessing it is simple! Just direct your browser to the URL mapping you created by combining the URL of your BBjServices host, the context, and your custom BBxServlet mapping. For instance, if you are running BBjServices locally, go to:


Example Servlets

The "Simple BBxServlet" below simply writes some HTML to the BBxServletResponse object, which will be displayed when a user navigates to the URL mapping the servlet is registered to.


CLASS PUBLIC ExampleServlet  
    method public void service(BBxServletContext context!) 
        request! = context!.getRequest()
        response! = context!.getResponse()
        session! = request!.getSession()

        s! = response!.getOutputStream()
        s!.write("<h1>This will print</h1>")


The "Complex BBxServlet" below can be used to test URL parameters and session operations.

The URL pattern for the servlet is:


The BBxServlet will print out the parameters added after the URL, along with some session information.

The servlet will also carry out one of three special session operations if you specify the special parameter sessionOpt with a value of ADD, REMOVE, or INVALIDATE.

  • The ADD operation adds any parameters in the URL to the session attribute hashmap.

  • The REMOVE operation removes any parameters in the URL from the session attribute hashmap.

  • The INVALIDATE operation invalidates the session cookie in the client browser, creating a new session and setting a new session cookie.


CLASS PUBLIC TestingServlet
  METHOD PUBLIC void service(BBxServletContext context!)
    REM  Get the request and response object
    request! = context!.getRequest()
    response! = context!.getResponse()
    REM Get the HttpSession
    session! = request!.getSession()
    sessionOpt! = "NONE"
    if str(request!.getParameter("sessionOpt"))<>""
      sessionOpt! = request!.getParameter("sessionOpt")
    if sessionOpt!.equals("INVALIDATE") then
        session! = request!.getSession()
    out! = response!.getOutputStream()
    out!.write("<p>Session Operation: "+sessionOpt!+"</p>")
    paramNames! = request!.getParameterNames()
    if paramNames!.size()>0 then
      FOR N=0 TO paramNames!.size()-1
        paramName! = paramNames!.getItem(N)
        if sessionOpt!.equals("ADD") then
          session!.setAttribute(paramName!, request!.getParameter(paramName!))
        if sessionOpt!.equals("REMOVE") then
      NEXT N
      setMaxInter! = request!.getParameter("setMaxInactiveInterval")
    if setMaxInter!<>""
    out!.write("<h1>SESSION INFO:</h1>")
    if session!.isNew()
        out!.write("<li>NEW SESSION</li>")
    out!.write("<li>Creation time: "+str(session!.getCreationTime())+"</li>")
    out!.write("<li>Session ID: "+session!.getId()+"</li>")
    out!.write("<li>Last access time: "+str(session!.getLastAccessedTime())+"</li>")
    out!.write("<li>Max inactive interval: "+str(session!.getMaxInactiveInterval())+"</li>")
    out!.write("<h2>Session attributes:</h2>")
    attrNames! = session!.getAttributeNames()
    if attrNames!.size() > 0 
      FOR N=0 TO attrNames!.size()-1
        attrName! = attrNames!.get(N)
      next N


See Also