BBxServlet

Description

In BBj 21.00 and higher, the BBxServlet class provides improved servlet functionality that was previously offered by BBjServlet and BBjspServlet. BBxServlets are designed to offer more intuitive deployment, better usage of licenses, improved error handling, and longevity of support, while providing all of the functionality of previous servlet implementations.

A BBxServlet requires a BBj source file with the following:

  • A public class that defines the servlet

  • A method in that class that returns void and takes a BBxServletContext as its only argument. This method is called the service method.

    • Note that in the examples below, this method is named service, but any valid method name would work.

To register a BBxServlet, you must also configure the following:

  • A URL mapping to provide access to the servlet

  • A config file for the servlet to use

  • A status indicating whether the servlet is enabled

See the BBxServlet Tutorial for a guide to the creation, deployment, and usage of BBxServlets.

Creation

BBjAPI > BBjAdmin > BBjJettyServerConfiguration > BBjJettyContextConfiguration > BBxServlet

The BBxServlet object is created through the following BBjJettyContextConfiguration methods:

Return Value Method
BBxServlet BBjJettyContextConfiguration::addBBxServlet(String mapping, String source, String classname)
BBxServlet BBjJettyContextConfiguration::addBBxServlet(String mapping, String source, String classname, String method)
BBxServlet BBjJettyContextConfiguration::addBBxServlet(String mapping, String source, String classname, String method, String config)
BBxServlet BBjJettyContextConfiguration::addBBxServlet(String mapping, String source, String classname, String method, int enabled)
BBxServlet BBjJettyContextConfiguration::addBBxServlet(String mapping, String source, String classname, String method, String config, int enabled)
BBxServlet

BBjJettyContextConfiguration::addBBxServlet(String classname, String mapping, String source)

(Alternate argument order for compatibility with BBjspServlets)

BBxServlet

BBjJettyContextConfiguration::addBBxServlet(String classname, String mapping, String source, String config)

(Alternate argument order for compatibility with BBjspServlets)

Methods of BBxServlet

Return Value Method
void

addParam(String name, String value)

(Deprecated method to facilitate migration from BBjspServlet)

void addParameter(String name, String value)
void clearParameters()
void

clearParams()

(Deprecated method to facilitate migration from BBjspServlet)

void disable()
void enable()
String getClassName()
String getClassPath()
String

getConfig()

(Deprecated method to facilitate migration from BBjspServlet)

String getConfigFile()
String getMapping()
String getMethod()
String getParameter(String name)
BBjVector getParameterNames()
String getSourceFile()
String

getSourceName()

(Deprecated method to facilitate migration from BBjspServlet)

String getTerminalAlias()
String getUser()
String getWorkingDir()
boolean isEnabled()
void setClassName(String className)
void setClassPath(String sscp)
void

setConfig(String config)

(Deprecated method to facilitate migration from BBjspServlet)

void setConfigFile(String config)
void

setEnabled(boolean enabled)

(Deprecated method to facilitate migration from BBjspServlet)

void setMethod(String method)
void setSourceFile(String source)
void setTerminalAlias(String alias)
void setUser(String user)
void setWorkingDir(String dir)

Examples

BBxServlet_Simple.bbx

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

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

    METHODEND
CLASSEND

BBxServlet_Complex.bbx

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")
    fi
    
    if sessionOpt!.equals("INVALIDATE") then
        session!.invalidate()
        session! = request!.getSession()
    endif
    
    out! = response!.getOutputStream()
      
    out!.write("<html>")
    out!.write("<body>")
    out!.write("<p>Session Operation: "+sessionOpt!+"</p>")
    out!.write("<h1>Parameters:</h1>")
      
    paramNames! = request!.getParameterNames()
    
    if paramNames!.size()>0 then
      FOR N=0 TO paramNames!.size()-1
        paramName! = paramNames!.getItem(N)
        out!.write(paramName!+"="+request!.getParameter(paramName!)+"</br>")
                
        if sessionOpt!.equals("ADD") then
          session!.setAttribute(paramName!, request!.getParameter(paramName!))
        fi
        
        if sessionOpt!.equals("REMOVE") then
          session!.removeAttribute(paramName!)
        fi
                    
      NEXT N
        
    else
      out!.write("NONE")
    fi
    
      setMaxInter! = request!.getParameter("setMaxInactiveInterval")
        
    if setMaxInter!<>""
       session!.setMaxInactiveInterval(Integer.parseInt(setMaxInter!))
    endif
    
    out!.write("<h1>SESSION INFO:</h1>")
    out!.write("<ul>")
    
    if session!.isNew()
        out!.write("<li>NEW SESSION</li>")
    endif
    
    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("</ul>")
    
    out!.write("<h2>Session attributes:</h2>")
    out!.write("<ul>")
    attrNames! = session!.getAttributeNames()
    
    if attrNames!.size() > 0 
      FOR N=0 TO attrNames!.size()-1
        attrName! = attrNames!.get(N)
        out!.write("<li>"+attrName!+"="+str(session!.getAttribute(attrName!))+"</li>")
      next N
      
    else
      out!.write("NONE")
    fi
    
    out!.write("</ul>")
    out!.write("</body>")
    out!.write("</html>")

  METHODEND
CLASSEND

See Also

See the BBj Object Diagram for an illustration of the relationship between BBj Objects.

BBjAPI

BBxServlet Tutorial