Description
In BBj 21.00 and higher, this method sets the preferred buffer size for the body of the response. The servlet container will use a buffer at least as large as the size requested. The actual buffer size used can be found using getBufferSize.
A larger buffer allows more content to be written before anything is actually sent, thus providing the servlet with more time to set appropriate status codes and headers. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.
This method must be called before any response body content is written; if content has been written or the response object has been committed, this method throws an IllegalStateException.
Syntax
Return Value |
Method |
void
|
setBufferSize(int size) |
Parameters
Parameter |
Description |
size |
The preferred buffer size |
Return Value
None.
Remarks
This method is inherited from the Java HttpServletResponse object.
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
FI
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>.")
FI
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!)
FI
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!)
FI
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!)
FI
REM Check parameters for setContentType and set it to the specified value
setContentType! = request!.getParameter("setContentType")
IF setContentType!.length() > 0 THEN
response!.setContentType(setContentType!)
FI
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!))
FI
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!)
FI
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!))
FI
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>"
FI
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
|
- BBj 21.0: BBxServlet introduced.
See Also
See the BBj Object Diagram for an illustration of the relationship between BBj Objects.
BBjAPI
BBxServlet
BBxServlet Tutorial
BBxServletResponse
BBxServletRequest