BBjJettyContext

Description

In BBj 15.00 and higher, BBjJettyContext stores context specific attributes and parameters that are available to all servlets running within that context.

Creation

BBxServlet > BBxServletContext > BBxWebSession > BBjJettyContext

A BBjCookie object is obtained with the following BBxWebSession method:

Return Value

Method

BBjJettyContext

getContext()

Deprecated Creation

BBjServletEvent > BBjHttpRequest > BBjHttpSession > BBjJettyContext

Return Value

Method

BBjJettyContext

BBjHttpSession::getContext()
BBjJettyContext BBjspWebSession::getContext()

Methods of BBjJettyContext

Return Value

Method

boolean

contains(string name)

boolean

containsAttribute(string name)

object

getAttribute(string name)

BBjVector

getAttributeNames()

string

getConfig()

String

getContextName()

String getContextPath()

string

getDocBase()

BBjString

getInitParameter(string name)

BBjVector

getInitParameterNames()

BBjspLogger getLogger()
String getServletMapping()

void

removeAttribute(string name)

void

setAttribute(string name, Object value)

Remarks

Parameters are defined with the context in jetty.xml and are read-only. Attributes can be added and removed at runtime. The example below demonstrates the use of all the methods for accessing context -specific data.

Example

declare BBjServletData data!

data! = BBjAPI().getServletData()

declare MyServlet myServlet!

myServlet! = new MyServlet()

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

PROCESS_EVENTS

class public MyServlet

 field private BBjNumber chan
 field private BBjHttpSession session!
 field private BBjHttpRequest request!
 field private BBjJettyContext context!

 method public void myMethod(BBjServletEvent p_event!)

   #chan = UNT
   #request! = cast(BBjHttpRequest,p_event!.getHttpRequest())
   #session! = cast(BBjHttpSession,#request!.getSession())
   #context! = cast(BBjJettyContext,#session!.getContext())

   response! = p_event!.getHttpResponse()
   response!.setContentType("text/html")

   open(#chan)"JSERVLET"

   print(#chan)"<html>"
   print(#chan)"<body>"
   print(#chan)"<h1>BBj Context Test</h1>"
   print(#chan)"<h3>Running in context <b>"
   print(#chan)#context!.getContextName()
   print(#chan)"</b></h3>"
   print(#chan)"<hr>"

   #processChanges()
   #renderInitParameters()
   print(#chan)"<hr>"
   #renderAttributes()

   print(#chan)"</body>"
   print(#chan)"</html>"
   close(#chan)
 methodend

 

 method private void processChanges()
   if #request!.getParameterNames().contains("add") then
     n$ = #request!.getParameter("name")
     v$ = #request!.getParameter("value")
     #context!.setAttribute(n$,v$)
   endif

   #context!.removeAttribute(#request!.getParameter("delete"))
 methodend  

 method private void renderInitParameters()

   vectParamNames! = #context!.getInitParameterNames()

   if vectParamNames!.size() then
     print(#chan)"<table>"
     print(#chan)"<tr>"
     print(#chan)"<td>Parameter Name</td>"
     print(#chan)"<td>&nbsp;</td>"
     print(#chan)"<td>Value</td>"
     print(#chan)"</tr>"

     for i = 0 to vectParamNames!.size() -1
       n$ = vectParamNames!.get(i)
       v$ = cast(BBjString,#context!.getInitParameter(n$))
       print(#chan)"<tr>"
       print(#chan)"<td>" + n$ + "</td>"
       print(#chan)"<td/>"
       print(#chan)"<td>" + v$ + "</td>"
       print(#chan)"</tr>"
     next i

     print(#chan)"</table>"
   endif
 methodend

 method public void renderAttributes()
   vectAttribNames! = #context!.getAttributeNames()

   print(#chan)"<table>"
   print(#chan)"<tr>"
   print(#chan)"<td>Attribute Name</td>"
   print(#chan)"<td>&nbsp;</td>"
   print(#chan)"<td>value</td>"
   print(#chan)"<td/>"
   print(#chan)"</tr>"

   if vectAttribNames!.size() then
     for i = 0 to vectAttribNames!.size() -1
       n$ = vectAttribNames!.get(i)
       v$ = cast(BBjString,#context!.getAttribute(n$))

       print(#chan)"<tr>"
       print(#chan)"<td>" + n$ + "</td>"
       print(#chan)"<td />"
       print(#chan)"<td>" + v$ +"</td>"
       print(#chan)"<form method='post'>"
       print(#chan)"<td>"
       print(#chan)"<input type='hidden' name='delete' value='"+n$+"'>"
       print(#chan)"<input type='submit' value='DEL'/>"
       print(#chan)"</td>"
       print(#chan)"</form>"
       print(#chan)"</tr>"
     next i
   endif

   print(#chan)"<form method='post'>"
   print(#chan)"<tr>"
   print(#chan)"<td><input name='name' /></td>"
   print(#chan)"<td />"
   print(#chan)"<td><input name='value'/></td>"
   print(#chan)"<td><input type='submit' name='add' value='ADD'/></td>"
   print(#chan)"</tr>"
   print(#chan)"</form>"
   print(#chan)"</table>"
 methodend
classend 

See Also

BBjAPI

BBxServletContext

BBxServletRequest

BBxWebSession

BBjJettyContextConfiguration

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