BBxServletRequest::getServletPath

Description

In BBj 21.00 and higher, this method returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.

This method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.

Syntax

Return Value Method
string getServletPath()

Parameters

None.

Return Value

A String containing the name or path of the servlet being called, as specified in the request URL, decoded, or an empty string if the servlet used to process the request is matched using the "/*" pattern.

Remarks

This method is inherited from the Java HttpServletRequest 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/BBxServletRequestDemo.bbj"

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

CLASS PUBLIC BBxServletRequestDemo

    REM Demonstration of various methods and functions of the BBxServletRequest object
    METHOD PUBLIC VOID bbxServletRequestDemo(BBxServletContext context!)
        
        REM Retrieve the BBxServletRequest from BBxServletContext
        request! = context!.getRequest()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = context!.getResponse().getOutputStream()
        
        out!.write("<h2>BBxServletRequest</h2>")
        out!.write("<p>The BBxServletRequest is used to retrieve information about the web request made to the servlet.")
        out!.write("</p>")
        
        #requestParametersDemo(context!)
        out!.write("<p>See <code>requestParametersDemo</code> in source code for method use examples.</p>")
        
        #requestAttributesDemo(context!)
        out!.write("<p>See <code>requestAttributesDemo</code> in source code for method use examples.</p>")
        
        #requestCookiesDemo(context!)
        out!.write("<p>See <code>requestCookiesDemo</code> in source code for method use examples.</p>")

        #fileUploadsDemo(context!)
        out!.write("<p>See <code>fileUploadsDemo</code> in source code for method use examples.</p>")
        
        #requestInfoMethodsDemo(context!)
        out!.write("<p>See <code>requestInfoMethodsDemo</code> in source code for method use examples.</p>")
    METHODEND
    
    REM Demonstrates methods used to interact with request attributes including getAttribute(String name), getAttributeNames(), removeAttribute(String name), and setAttribute(String name, Object value).
    METHOD PUBLIC VOID requestAttributesDemo(BBxServletContext context!)
        
        REM  Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = context!.getResponse().getOutputStream()
        
        REM Check if there are any attributes to add
        attributeNamesToAdd! = request!.getParameterValues("addRequestAttrName")
        attributeValuesToAdd! = request!.getParameterValues("addRequestAttrVal")
        IF attributeNamesToAdd!.size()>0 THEN
            FOR N=0 TO attributeNamesToAdd!.size()-1
                name$ = attributeNamesToAdd!.getItem(N)
                value$ = attributeValuesToAdd!.getItem(N)
                
                REM set the attribute in the request
                request!.setAttribute(name$, value$)
                
            NEXT N
        FI
        
        REM Check if there are any attributes to remove
        attributesToRemove! = request!.getParameterValues("removeRequestAttrName")
        IF attributesToRemove!.size()>0 THEN
            FOR N=0 TO attributesToRemove!.size()-1
                name$ = attributesToRemove!.getItem(N)
            
                REM remove the attribute from the request
                request!.removeAttribute(name$)
                
            NEXT N
        FI
        
        REM Get request attribute names
        attributeNames! = request!.getAttributeNames()
        
        REM Get request attribute values for each attribute name
        attributeValuesString!= ""
        
        IF attributeNames!.size() > 0 THEN
            FOR N=0 TO attributeNames!.size()-1
                attributeName$ = attributeNames!.getItem(N)
                
                REM retrieve attribute value with attribute name
                attributeValue$ = request!.getAttribute(attributeName$)
                
                REM Attribute name and value to output string
                attributeValuesString! = attributeValuesString!+"<li>getRequestAttribute(<em>"+attributeName$+"</em>) = "+attributeValue$+"</li>"
            NEXT N
        ELSE
            attributeValuesString! = "<li>No request attributes set</li>"
        FI
        
        REM Output text to servlet response output stream
        out!.write("<h3>Request Attributes</h3>")
        out!.write("<p>Request attributes are objects that are sent along with the request. The following are the ")
        out!.write("methods that can be used to interact with request attributes along with their return values for ")
        out!.write("this particular BBxServletRequest if they exist:")
        out!.write("<code><ul>")
        out!.write("<li>BBjVector getAttributeNames = "+attributeNames!.toString()+"</li>")
        out!.write("<li>Object getAttribute(name$):<ul>")
        out!.write(attributeValuesString!) 
        out!.write("</ul></li>")
        out!.write("<li>void setAttribute(String name$, Object value!)</li>")
        out!.write("<li>void removeAttribute(String name$)</li>")
        out!.write("</ul></code>")
        
        REM An HTML form to add attributes to the request
        out!.write("Enter the name and value of a request attribute to add:")
        out!.write("<form method='GET' enctype='multipart/form-data' action='"+request!.getServletPath()+"'>")
        out!.write("Name: <input type='text' name='addRequestAttrName'> Value: <input type='text' name='addRequestAttrVal'> ")
        out!.write("<input type='submit' value='Press'> to <b>add</b> an attribute to the request!")
        out!.write("</form>")
        
        REM An HTML form to remove attributes from the request
        out!.write("Enter the name of a request attribute to remove:")
        out!.write("<form method='GET' enctype='multipart/form-data' action='"+request!.getServletPath()+"'>")
        out!.write("Name: <input type='text' name='removeRequestAttrName'> ")
        out!.write("<input type='submit' value='Press'> to <b>remove</b> an attribute from the request!")
        out!.write("</form>")
        
    METHODEND
    
    REM Demonstrates methods that are used in BBxServletRequest to interact with request parameters including getParameterNames() and getParameter(String name)
    METHOD PUBLIC VOID requestParametersDemo(BBxServletContext context!)
        
        REM  Get the request object from the BBxServletContext
        request! = context!.getRequest() 
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = context!.getResponse().getOutputStream()
        
        REM Retrieve request parameter names
        paramNames! = request!.getParameterNames()
        
        REM Retrieve request parameter values
        paramValuesString! = ""
        IF paramNames!.size()>0 then
            FOR N=0 TO paramNames!.size()-1
                paramName! = paramNames!.getItem(N)
                paramValue! = request!.getParameter(paramName!)
                paramValuesString! = paramValuesString! + "<li>getParameter(<em>"+paramName!+"</em>) = "+paramValue!+"</li>"
            NEXT N
        ELSE
            paramValuesString! = "<li>There are currently no request parameters in the URL</li>"
        FI
    
        out!.write("<h3>Request Parameters</h3>")
        
        out!.write("<p>These are the queries that are added to the end of the request URL</br>")
        out!.write("Example: In the URL <code>https://domain.com/servletPath?query1=value&query2=value2</code>, the ")
        out!.write("parameters would be <code>query1</code> and <code>query2</code> with their values being ")
        out!.write("<code>value</code> and <code>value2</code>.</p>")
        
        out!.write("The following are the method names and return values for BBxServletRequest methods that interact ")
        out!.write("with request parameters.")
        out!.write("<code><ul>")
        out!.write("<li>BBjVector getParameterNames() = "+paramNames!.toString()+"</li>")
        out!.write("<li>String getParameter(String name$): <ul>"+paramValuesString!+"</ul>")
        out!.write("</ul></code>")
    METHODEND
    
    REM Demonstration of how to retrieve uploaded file information from BBxServletResponse
    METHOD PUBLIC VOID fileUploadsDemo(BBxServletContext context!)
        REM  Get the request object from the BBxServletContext
        request! = context!.getRequest()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = context!.getResponse().getOutputStream()
        
        REM getFileUploads() returns a BBjVector of the BBjFileUploads that were sent along with the request
        uploads! = request!.getFileUploads()
        
        out!.write("<h3>BBjFile Uploads</h3>")
        out!.write("<p>File uploads are saved in [BBJ_HOME]/cache/servletUploads and can be posted to a servlet ")
        out!.write("with a 'POST' request.</p>")
        
        out!.write("<p>In order to retrieve uploaded file information, getFileUploads can be called which returns a ")
        out!.write("BBjVector of BBjFileUpload objects.  Below are the files uploaded with this request.</p><code><ul>")    
        IF uploads!.size() > 0 THEN
            FOR N=0 TO uploads!.size()-1
                upload! = uploads!.getItem(N)
                out!.write("<li>File "+String.valueOf(N+1)+":")
                out!.write("<ul><li>Name: "+upload!.getOriginalName())
                out!.write("<li>Location: "+upload!.getTempName()+"</li></ul>")
            NEXT N
        
        ELSE
            out!.write("There were no files uploaded with this request.  Try uploading one below!")
        FI
        
        out!.write("</ul></code>")
        
        REM This part creates the POST request form and sends it back to the servlet
        out!.write("<p>Try uploading a file below</p>")
        out!.write("<form method='POST' enctype='multipart/form-data' action='"+request!.getServletPath()+"'>")
        out!.write("File to upload: <input type='file' name='upfile'><br/>")
        out!.write("<br/>")
        out!.write("<input type='submit' value='Press'> to upload the file!")
        out!.write("</form>")
        
    METHODEND
    
    REM Demonstration of how to interact with web cookies through BBxServletRequest
    METHOD PUBLIC VOID requestCookiesDemo(BBxServletContext context!)
        REM  Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = context!.getResponse().getOutputStream()
        
        REM Here is where the cookies are retrieved
        cookies! = request!.getCookies()
        
        out!.write("<h3>Request Cookies</h3>")
        out!.write("<p>Client-side cookies are sent along with incoming requests, allowing the servlet to receive ")
        out!.write("information about the client sending the request.</p>")
        out!.write("<p>Cookies can be retrieved from the BBxServletRequest with getCookies(), which returns a BBjVector ")
        out!.write("of BBjCookies inside of the request.  Below are the cookies set along with this request.</p>")
        out!.write("<ul>")
        
        REM loop over cookies and print out their information
        IF cookies!.size() > 0 THEN
            FOR N=0 TO cookies!.size()-1
                cookie! = cookies!.getItem(N)
                out!.write("<li>Cookie "+String.valueOf(N+1))
                
                out!.write("<code><ul>")
                out!.write("<li>Name: "+cookie!.getName()+"</li>")
                out!.write("<li>Value: "+cookie!.getValue()+"</li>")
                out!.write("</ul></code>")
                
                out!.write("</li>")
            NEXT N
        
        ELSE
            out!.write("<code>There are currently no cookies set for this servlet. <strong>Try refreshing.</strong></code>")
        FI
        
        out!.write("</ul>")
    METHODEND
    
    REM Demonstration of various other methods used to interact with BBxServletRequest
    METHOD PUBLIC VOID requestInfoMethodsDemo(BBxServletContext context!)
        REM  Get the request object from the BBxServletContext
        request! = context!.getRequest()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = context!.getResponse().getOutputStream()
        
        REM Retrieve return values of each of the informational methods

        authType! = request!.getAuthType()
        body! = request!.getBody()
        characterEncoding! = request!.getCharacterEncoding()
        contentLength! = request!.getContentLength()
        contentType! = request!.getContentType()
        contextName! = request!.getContextName()
        contextPath! = request!.getContextPath()
        headerNames! = request!.getHeaderNames()
        
        REM Build strings for header values
        getHeaderString$ = ""
        getHeadersString$ = ""
        IF headerNames!.size()>0 then
            FOR N=0 TO headerNames!.size()-1
                headerName$ = headerNames!.getItem(N)
                
                headerVal$ = request!.getHeader(headerName$)
                headersVector! = request!.getHeaders(headerName$)
                
                getHeaderString$ = getHeaderString$ + "<li>getHeader(<em>"+headerName$+"</em>) = "+headerVal$+"</li>"
                getHeadersString$ = getHeadersString$ + "<li>getHeaders(<em>"+headerName$+"</em>) = "+headersVector!.toString()+"</li>"
            NEXT N
        FI
        
        localAddr! = request!.getLocalAddr()
        locale! = request!.getLocale()
        locales! = request!.getLocales()
        localName! = request!.getLocalName()
        localPort! = request!.getLocalPort()
        method! = request!.getMethod()
        pathInfo! = request!.getPathInfo()
        protocol! = request!.getProtocol()
        queryString! = request!.getQueryString()
        remoteAddr! = request!.getRemoteAddr()
        remoteHost! = request!.getRemoteHost()
        remotePort! = request!.getRemotePort()
        remoteUser! = request!.getRemoteUser()
        requestedSessionId! = request!.getRequestedSessionId()
        requestURI! = request!.getRequestURI()
        requestURL! = request!.getRequestURL()
        scheme! = request!.getScheme()
        serverName! = request!.getServerName()
        serverPort! = request!.getServerPort()
        servletPath! = request!.getServletPath()
        isRequestedSessionIdFromCookie! = request!.isRequestedSessionIdFromCookie()
        isRequestedSessionIdFromURL! = request!.isRequestedSessionIdFromURL()
        isRequestedSessionIdValid! = request!.isRequestedSessionIdValid()
        isSecure! = request!.isSecure()
        
        out!.write("<h3>Request Info Methods</h3>")
        out!.write("<p>These are various methods that have been inherited from javax.servlet.http.HttpServletRequest ")
        out!.write("along with their return values</p>")
        
        REM Output values to response output stream
        REM NOTE: To avoid NullPointerExceptions, all values will be cast to a java.lang.Object and String.valueOf() will be called to receive a string of "null" instead of null values
        out!.write("<ul><code>")
        out!.write("<li>String    getAuthType() = "+String.valueOf(CAST(java.lang.Object, authType!))+"</li>")
        out!.write("<li>String    getBody() = "+String.valueOf(CAST(java.lang.Object, body!))+"</li>")
        out!.write("<li>String    getCharacterEncoding() = "+String.valueOf(CAST(java.lang.Object, characterEncoding!))+"</li>")
        out!.write("<li>int       getContentLength() = "+String.valueOf(contentLength!)+"</li>")
        out!.write("<li>String    getContentType() = "+String.valueOf(CAST(java.lang.Object, contentType!))+"</li>")
        out!.write("<li>String    getContextName() = "+String.valueOf(CAST(java.lang.Object, contextName!))+"</li>")
        out!.write("<li>String    getContextPath() = "+String.valueOf(CAST(java.lang.Object, contextPath!))+"</li>")
        out!.write("<li>long      getDateHeader(</code>''<code>If-Modified-Since</code>''<code>) = "+String.valueOf(request!.getDateHeader("If-Modified-Since"))+" </li>")
        out!.write("<li>BBjVector getHeaderNames() = "+headerNames!.toString()+"</li>")
        out!.write("<li>String    getHeader(headerName$) = <ul>"+getHeaderString$+"</ul></li>")
        out!.write("<li>BBjVector getHeaders(headerName$) = <ul>"+getHeadersString$+"</ul></li>")
        out!.write("<li>String    getLocalAddr() = "+String.valueOf(CAST(java.lang.Object, localAddr!))+"</li>")
        out!.write("<li>Locale    getLocale() = "+String.valueOf(CAST(java.lang.Object, locale!))+"</li>")
        out!.write("<li>BBjVector getLocales() = "+String.valueOf(CAST(java.lang.Object, locales!))+"</li>")
        out!.write("<li>String    getLocalName() = "+String.valueOf(CAST(java.lang.Object, localName!))+"</li>")
        out!.write("<li>int       getLocalPort() = "+String.valueOf(CAST(java.lang.Object, localPort!))+"</li>")
        out!.write("<li>String    getMethod() = "+String.valueOf(CAST(java.lang.Object, method!))+"</li>")
        out!.write("<li>String    getPathInfo() = "+String.valueOf(CAST(java.lang.Object, pathInfo!))+"</li>")
        out!.write("<li>String    getProtocol() = "+String.valueOf(CAST(java.lang.Object, protocol!))+"</li>")
        out!.write("<li>String    getQueryString() = "+String.valueOf(CAST(java.lang.Object, queryString!))+"</li>")
        out!.write("<li>String    getRemoteAddr() = "+String.valueOf(CAST(java.lang.Object, remoteAddr!))+"</li>")
        out!.write("<li>String    getRemoteHost() = "+String.valueOf(CAST(java.lang.Object, remoteHost!))+"</li>")
        out!.write("<li>int       getRemotePort() = "+String.valueOf(CAST(java.lang.Object, remotePort!))+"</li>")
        out!.write("<li>String    getRemoteUser() = "+String.valueOf(CAST(java.lang.Object, remoteUser!))+"</li>")
        out!.write("<li>String    getRequestedSessionId() = "+String.valueOf(CAST(java.lang.Object, requestedSessionId!))+"</li>")
        out!.write("<li>String    getRequestURI() = "+String.valueOf(CAST(java.lang.Object, requestURI!))+"</li>")
        out!.write("<li>String    getRequestURL() = "+String.valueOf(CAST(java.lang.Object, requestURL!))+"</li>")
        out!.write("<li>String    getScheme() = "+String.valueOf(CAST(java.lang.Object, scheme!))+"</li>")
        out!.write("<li>String    getServerName() = "+String.valueOf(CAST(java.lang.Object, serverName!))+"</li>")
        out!.write("<li>int       getServerPort() = "+String.valueOf(request!.getServerPort())+"</li>")
        out!.write("<li>String    getServletPath() = "+String.valueOf(CAST(java.lang.Object, servletPath!))+"</li>")
        out!.write("<li>boolean   isRequestedSessionIdFromCookie() = "+Boolean.toString(isRequestedSessionIdFromCookie!)+"</li>")
        out!.write("<li>boolean   isRequestedSessionIdFromURL() = "+Boolean.toString(isRequestedSessionIdFromURL!)+"</li>")
        out!.write("<li>boolean   isRequestedSessionIdValid() = "+Boolean.toString(isRequestedSessionIdValid!)+"</li>")
        out!.write("<li>boolean  isSecure() = "+Boolean.toString(isSecure!)+"</li>")
        out!.write("</ul></code>")  
    METHODEND
CLASSEND

ClosedVersion History

  • BBj 21.00: BBxServlet introduced.

See Also

BBjAPI

BBxServlet

BBxServlet Tutorial

BBxServletRequest

BBxServletResponse

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