AB Infinity feb 2014 | Página 3

© Prentice Hall and Sun Microsystems. Personal use only; do not redistribute. 116 Chapter 5 Accessing the Standard CGI Variables 5.1 Servlet Equivalent of CGI Variables For each standard CGI variable, this section summarizes its purpose and the means of accessing it from a servlet. As usual, once you are familiar with this information, you may want to use Appendix A (Servlet and JSP Quick Reference) as a reminder. Assume request is the HttpServletRequest supplied to the doGet and doPost methods. AUTH_TYPE If an Authorization header was supplied, this variable gives the scheme specified (basic or digest). Access it with request.getAuthType(). CONTENT_LENGTH For POST requests only, this variable stores the number of bytes of data sent, as given by the Content-Length request header. Technically, since the CONTENT_LENGTH CGI variable is a string, the servlet equivalent is String.valueOf(request.getContentLength()) or request.getHeader("Content-Length"). You'll probably want to just call request.getContentLength(), which returns an int. CONTENT_TYPE CONTENT_TYPE designates the MIME type of attached data, if specified. See Table 7.1 in Section 7.2 (HTTP 1.1 Response Headers and Their Meaning) for the names and meanings of the common MIME types. Access CONTENT_TYPE with request.getContentType(). DOCUMENT_ROOT The DOCUMENT_ROOT variable specifies the real directory corresponding to the URL http://host/. Access it with getServletContext().getRealPath("/"). In older servlet specifications you accessed this variable with request.getRealPath("/"); the older access method is no longer supported. Also, you can use getServletContext().getRealPath to map an arbitrary URI (i.e., URL suffix that comes after the hostname and port) to an actual path on the local machine. Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com. Servlet and JSP training courses by book’s author: courses.coreservlets.com.