BBxServletResponse::createCookie

Description

In BBj 21.00 and higher, this method creates and returns a new BBjCookie with the specified name.

Syntax

Return Value Method
BBjCookie createCookie(string name)

Parameters

Parameter Description
name The name of the cookie.

Return Value

A new BBjCookie with the specified name.

Example

rem '/**

rem '* Enter the values for your username, password, and the filepath to this file
rem '*/
username$ = "admin"
password$ = "admin123"
pathToThisFile$ = "/path/to/BBxServletResponseDemo.bbj"

context! = BBjAPI().getAdmin(username$, password$).getJettyServerConfig().getCustomContext("root")
context!.addBBxServlet("/BBxServletResponseDemo", pathToThisFile$, "BBxServletResponseDemo", "bbxServletResponseDemo", 1)
BBjAPI().getAdmin(username$, password$).getJettyServer().getContext("root").restart()

CLASS PUBLIC BBxServletResponseDemo

    rem 'Demonstrates the various capabilities of the BBxServletResponse object
    METHOD PUBLIC VOID bbxServletResponseDemo(BBxServletContext context!)

        rem 'Get the response object from the BBxServletContext
        response! = context!.getResponse()

        rem 'Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()

        out!.write("<h2>BBxServletResponse</h2>")
        out!.write("<p>This is the response object that is used to send information back to the client when the ")
        out!.write("servlet is called. The primary method through which information is passed around is the ")
        out!.write("ServletOutputStream returned through BBxServletResponse.getOutputStream.</p>")

        #addCookieDemo(context!)
        out!.write("<p>See <code>addCookieDemo</code> in source code for method usage examples</p>")

        #statusCodesDemo(context!)
        out!.write("<p>See <code>statusCodeDemo</code> in source code for method usage examples</p>")

        #sendRedirectDemo(context!)
        out!.write("<p>See <code>sendRedirectDemo</code> in source code for method usage examples</p>")

        #responseInfoMethodsDemo(context!)
        out!.write("<p>See <code>responseInfoMethodsDemo</code> in source code for method usage examples</p>")

    METHODEND

    rem 'Demonstrates how to create a cookie as well as how to add it to the BBxServletResponse
    METHOD PUBLIC VOID addCookieDemo(BBxServletContext context!)

        rem 'Get the request and response object from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()

        rem 'Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()

        rem 'Check parameters for addCookie query
        cookies! = new BBjVector()
        cookiesToAdd! = request!.getParameterValues("addCookieName")
        cookieVals! = request!.getParameterValues("cookieVal")
        if (cookiesToAdd!.size()>0) then
            FOR N=0 TO cookiesToAdd!.size()-1
                name$ = cookiesToAdd!.getItem(N)
                value$ = cookieVals!.getItem(N)

                rem 'create cookie
                cookie! = response!.createCookie(name$)
                cookie!.setValue(value$)

                rem 'add cookie to response
                response!.addCookie(cookie!)

                cookies!.addItem(cookie!)
            NEXT N
        endif

        out!.write("<h3>Adding BBjCookies</h3>")
        out!.write("<p>BBjCookies can be added to a response which will set them in the client browser. This can be")
        out!.write(" done by creating a cookie with <code>response!.createCookie(name$)</code> and then adding the ")
        out!.write("cookie to the response with <code>response!.addCookie(cookie!)</code>.<p>")

        rem 'loop over cookie BBjVector to print out the cookies that will be added
        if cookies!.size() > 0
            out!.write("<p>The following cookies will be added to this response:</p><ul>")
            FOR N=0 TO cookies!.size()-1
                cookie! = cookies!.getItem(N)
                out!.write("<li> Cookie "+String.valueOf(N)+":")
                out!.write("<ul><li>name = "+cookie!.getName()+"</li><li>value = "+cookie!.getValue()+"</li></ul>")
            NEXT N
            out!.write("</ul>")
            out!.write("To see these cookies in the request, <b>refresh the page</b>.")
        endif

        out!.write("<p>Try adding a cookie below!</p>")

        rem 'An HTML form to add cookies to the request
        out!.write("Enter the name and value of a cookie to add:")
        out!.write("<form method='GET' enctype='text/html' action='"+request!.getServletPath()+"'>")
        out!.write("Name: <input type='text' name='addCookieName'> Value: <input type='text' name='cookieVal'> ")
        out!.write("<input type='submit' value='Press'> to <b>add</b> a cookie to the next response!")
        out!.write("</form>")

    METHODEND

    rem 'Demonstrates the getStatus() and sendError(in code) methods in BBxServletResponse
    METHOD PUBLIC VOID statusCodesDemo(BBxServletContext context!)

        rem 'Get the request and response object from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()

        rem 'Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()

        rem 'Check parameters for sendError query
        errorCode! = request!.getParameter("sendError")
        errorMsg! = request!.getParameter("errorMsg")
        if (errorCode!.length() > 0) then
            rem 'Send the error code and message
            response!.sendError(Integer.parseInt(errorCode!), errorMsg!)

        endif

        rem 'Get the value of getStatus()
        status! = response!.getStatus()

        out!.write("<h3>Status Codes</h3>")
        out!.write("<p>Through BBxServletResponse, you have the capability to check status codes as well as send error ")
        out!.write("codes along with a message.  Below is the current return value for <code>getStatus()</code></p>")
        out!.write("<code><ul><li>getStatus() = "+String.valueOf(status!)+"</li></ul></code>")

        rem 'Form for submitting an error code
        out!.write("Try submitting an error code and message for yourself!")
        out!.write("<form method='GET' enctype='text/html' action='"+request!.getServletPath()+"'>")
        out!.write("Error Code: <input type='text' name='sendError'> Message: <input type='text' name='errorMsg'> ")
        out!.write("<input type='submit' value='Press'> to send an error.")
        out!.write("</form>")

    METHODEND

    rem 'Demonstration of the sendRedirect(url$) method
    METHOD PUBLIC VOID sendRedirectDemo(BBxServletContext context!)

        rem 'Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()

        rem 'Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()

        rem 'check parameters for sendRedirect parameter
        sendRedirect! = request!.getParameter("sendRedirect")

        rem 'Send the redirect on the response
        if (sendRedirect!.length() > 0) then
            response!.sendRedirect(sendRedirect!)
        endif

        out!.write("<h3>Send Redirect</h3>")
        out!.write("<p>With <code>sendRedirect</code>, you can redirect a page to any URL you would like.</p>")
        out!.write("<p>Enter a URL below to redirect the servlet to that page</p>")

        rem 'An HTML form to add sendRedirect parameter to URL
        out!.write("<form method='GET' enctype='text/html' action='"+request!.getServletPath()+"'>")
        out!.write("Redirect URL: <input type='text' name='sendRedirect'> ")
        out!.write("<input type='submit' value='Press'> to redirect page")
        out!.write("</form>")

    METHODEND

    rem 'Demonstrates methods for getting and setting various information in the BBxServletResponse
    METHOD PUBLIC VOID responseInfoMethodsDemo(BBxServletContext context!)

        rem 'Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()

        rem 'Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()

        rem 'Check parameters for setCharacterEncoding and set it to the specified value
        setCharacterEncoding! = request!.getParameter("setCharacterEncoding")
        if (setCharacterEncoding!.length() > 0) then
            response!.setCharacterEncoding(setCharacterEncoding!)
        endif

        rem 'Check parameters for setContentType and set it to the specified value
        setContentType! = request!.getParameter("setContentType")
        if (setContentType!.length() > 0) then
            response!.setContentType(setContentType!)
        endif

        rem 'Check parameters for setDateHeader values and set the date header
        setDateHeaderName! = request!.getParameter("setDateHeaderName")
        setDateHeaderValue! = request!.getParameter("setDateHeaderValue")
        if (setDateHeaderName!.length() > 0) then
            response!.setDateHeader(setDateHeaderName!, Long.parseLong(setDateHeaderValue!))
        endif

        rem 'Check parameters for setHeader values and set them in the response
        setHeaderName! = request!.getParameter("setHeaderName")
        setHeaderValue! = request!.getParameter("setHeaderValue")
        if (setHeaderName!.length() > 0) then
            response!.setHeader(setHeaderName!, setHeaderValue!)
        endif

        rem 'Check parameters for setLocale and set the locale to that value
        setLocale! = request!.getParameter("setLocale")
        if (setLocale!.length() > 0) then
            response!.setLocale(new java.util.Locale(setLocale!))
        endif

        rem 'Get BBxServletResponse info
        isCommitted! = response!.isCommitted()
        characterEncoding! = response!.getCharacterEncoding()
        contentType! = response!.getContentType()
        headerNames! = response!.getHeaderNames()

        getHeadersString$ = ""
        getHeaderString$ = ""
        if (headerNames!.size() > 0) then
            getHeadersString$ = getHeadersString$ + "<ul>"
            getHeaderString$ = getHeaderString$ + "<ul>"

            FOR N=0 TO headerNames!.size()-1
                name$ = headerNames!.get(N)

                headers! = response!.getHeaders(name$)
                getHeadersString$ = getHeadersString$ + "<li>getHeaders(<em>"+name$+"</em>) = "+String.valueOf(headers!)+"</li>"

                header! = response!.getHeader(name$)
                getHeaderString$ = getHeaderString$ + "<li>getHeaders(<em>"+name$+"</em>) = "+String.valueOf(header!)+"</li>"

            NEXT N

            getHeadersString$ = getHeadersString$ + "</ul>"
            getHeaderString$ = getHeaderString$ + "</ul>"
        endif

        locale! = response!.getLocale()

        out!.write("<h3>BBxServletResponse Header Info</h3>")
        out!.write("<p>BBxServletResponse has a number of methods that allow you to get as well as set values in the ")
        out!.write("response header. Below are the methods and return values along with fields that will allow you to")
        out!.write(" change their values.</p>")

        out!.write("<code><ul>")
        out!.write("<li>boolean isCommitted() = "+Boolean.toString(isCommitted!)+"</li>")
        out!.write("<li>String getCharacterEncoding() = "+String.valueOf(CAST(Object, characterEncoding!))+"</li>")
        out!.write("<li>String getContentType() = "+String.valueOf(CAST(Object, contentType!))+"</li>")
        out!.write("<li>Locale getLocale() = "+String.valueOf(CAST(Object, locale!))+"</li>")
        out!.write("<li>BBjVector getHeaderNames() = "+String.valueOf(headerNames!)+"</li>")
        out!.write("<li>String getHeader(String name) : "+getHeaderString$+"</li>")
        out!.write("<li>String getHeaders(String name) : "+getHeadersString$+"</li>")
        out!.write("</code></ul>")

        out!.write("<p>Try changing these values below.</p>")

        rem 'An HTML form to add setCharacterEncoding parameter to URL
        out!.write("<form method='GET' enctype='text/html' action='"+request!.getServletPath()+"'>")
        out!.write("Character Encoding: <input type='text' name='setCharacterEncoding'> ")
        out!.write("<input type='submit' value='Press'> to set character encoding")
        out!.write("</form>")

        rem 'An HTML form to add setContentType parameter to URL
        out!.write("<form method='GET' enctype='text/html' action='"+request!.getServletPath()+"'>")
        out!.write("Content Type: <input type='text' name='setContentType'> ")
        out!.write("<input type='submit' value='Press'> to set content type")
        out!.write("</form>")

        rem 'An HTML form to add setLocale parameter to URL
        out!.write("<form method='GET' enctype='text/html' action='"+request!.getServletPath()+"'>")
        out!.write("Locale: <input type='text' name='setLocale'> ")
        out!.write("<input type='submit' value='Press'> to set locale")
        out!.write("</form>")

        rem 'An HTML form to add setHeader parameters to URL
        out!.write("<form method='GET' enctype='text/html' action='"+request!.getServletPath()+"'>")
        out!.write("Header Name: <input type='text' name='setHeaderName'> Header Value: <input type='text' name='setHeaderValue'> ")
        out!.write("<input type='submit' value='Press'> to set header")
        out!.write("</form>")

    METHODEND
CLASSEND

ClosedVersion History

  • BBj 21.00: BBxServlet introduced.

See Also

BBjAPI

BBxServlet

BBxServlet Tutorial

BBxServletResponse

BBxServletRequest

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