BBxServletContext
Description
In BBj 21.00 and higher, the BBxServletContext object provides a context during the execution of a BBxServlet. When a BBxServlet runs, it receives a BBxServletContext object as a parameter in its service method.
The BBxServletContext object provides access to key elements of the BBxServlet and BBjServices, including the BBxServletRequest, BBxServletResponse, and BBxWebSession objects.
Creation
A BBxServletContext object is created when a BBxServlet is launched. It is received as a parameter in the BBxServlet's service method.
Methods of BBxServletContext
Example
REM /**
REM * Enter the values for your username, password, and the filepath to this file
REM */
username$ = "admin"
password$ = "admin123"
pathToThisFile$ = "/path/to/BBxServletContextDemo.bbj"
context! = BBjAPI().getAdmin(username$, password$).getJettyServerConfig().getCustomContext("root")
context!.addBBxServlet("/BBxServletContextDemo", pathToThisFile$, "BBxServletContextDemo", "bbxServletContextDemo", 1)
BBjAPI().getAdmin(username$, password$).getJettyServer().getContext("root").restart()
CLASS PUBLIC BBxServletContextDemo
REM Demonstration of various methods and functions of the BBxServletContext object
METHOD PUBLIC VOID bbxServletContextDemo(BBxServletContext context!)
REM The BBxServletRequest, BBxServletResponse, BBxWebSession, and BBjJettyContext are retrieved here
request! = context!.getRequest()
response! = context!.getResponse()
session! = context!.getSession()
bbjContext! = context!.getJettyContext()
REM Get ServletOutputStream from BBxServletResponse
out! = response!.getOutputStream()
REM Output Label for BBxServlet Context section of of servlet page
out!.write("<h2>BBxServletContext</h2>")
out!.write("<p>When a BBxServlet runs, it receives a BBxServletContext object as the sole parameter in its ")
out!.write("service method. The BBxServletContext object provides access to key elements of the BBxServlet ")
out!.write("and BBjServices, including the BBxServletRequest, BBxServletResponse, BBxWebSession, and ")
out!.write("BBjJettyContext objects. Demonstrated below is the additional information that can be accessed ")
out!.write("through BBxServletContext.</p>")
REM Method call for the getContextName() demo
#getContextNameDemo(context!)
out!.write("<p>See <code>getContextNameDemo</code> in source code for example.</p>")
REM Method call for demo of initialization parameter methods
#initParametersMethodsDemo(context!)
out!.write("<p>See <code>initParametersMethodsDemo</code> in source code for an example.</p>")
METHODEND
REM Demonstration of getContextName() method in BBxServletContext
METHOD PUBLIC VOID getContextNameDemo(BBxServletContext context!)
REM Get ServletOutputStream from BBxServletResponse
out! = context!.getResponse().getOutputStream()
REM Here is where we retrieve the context name for this servlet
contextName$ = context!.getContextName()
REM Output html for webpage that will be displayed
out!.write("<h3>Context Name</h3>")
out!.write("<p><code>getContextName()</code> returns a String representing the name of the context this ")
out!.write("servlet was added to. See below for the context name of this particular servlet.</p> ")
out!.write("<p><code>String getContextName() = "+contextName$+"</code></p>")
METHODEND
REM Demonstration of getInitParameter(String name) and getInitParameterNames() in BBxServletContext
METHOD PUBLIC VOID initParametersMethodsDemo(BBxServletContext context!)
REM Get ServletOutputStream from BBxServletResponse
out! = context!.getResponse().getOutputStream()
REM Get BBjVector of initialization parameter names
paramNames! = context!.getInitParameterNames()
REM Output initialization parameters label as well as
out!.write("<h3>Initialization Parameters:</h3>")
out!.write("<p>Initialization parameters are the parameters that were set at servlet creation time in either ")
out!.write("the EM or the BBj console. Below are the return values of <code>getInitParameterNames</code> and ")
out!.write("<code>getInitParamer(paramName$)</code> for this particular servlet.")
out!.write("<code>")
REM Create string to display return values
IF paramNames!.size()>0 THEN
out!.write("<ul>")
initParamValueString$ = ""
REM Loop over the parameter names and print them with their corresponding value
FOR N=0 TO paramNames!.size()-1
paramName$ = paramNames!.getItem(N)
REM Retrieve initialization parameter with the given name
paramValue$ = context!.getInitParameter(paramName$)
REM add value to string of parameter values
initParamValueString$ = initParamValueString$ + "<li>getInitParameter(<em>"+paramName$+"</em>) = "+paramValue$+"</li>"
NEXT N
REM Output parameter values to response output stream
out!.write("<li>BBjVector getInitParameterNames() = "+paramNames!.toString()+"</li>")
out!.write("<li>String getInitParameter(String name$):<ul>"+initParamValueString$+"</ul></li>")
out!.write("</ul>")
ELSE
out!.write("There are no initialization parameters for this servlet.")
FI
out!.write("</code>")
METHODEND
CLASSEND
|
See Also
BBxServlet
BBxServlet Tutorial
BBxServletRequest
BBxServletResponse
See the BBj Object Diagram for an illustration of the relationship between BBj Objects.