AB Infinity feb 2014 | Page 6

© Prentice Hall and Sun Microsystems. Personal use only; do not redistribute. 5.2 A Servlet That Shows the CGI Variables SERVER_PROTOCOL The SERVER_PROTOCOL variable indicates the protocol name and version used in the request line (e.g., HTTP/1.0 or HTTP/1.1). Access it by calling request.getProtocol(). SERVER_SOFTWARE This variable gives identifying information about the Web server. Access it by means of getServletContext().getServerInfo(). 5.2 A Servlet That Shows the CGI Variables Listing 5.1 presents a servlet that creates a table showing the values of all the CGI variables other than HTTP_XXX_YYY, which are just the HTTP request headers described in Chapter 4. Figure 5–1 shows the result for a typical request. Listing 5.1 ShowCGIVariables.java package coreservlets; import import import import java.io.*; javax.servlet.*; javax.servlet.http.*; java.util.*; /** Creates a table showing the current value of each * of the standard CGI variables. */ public class ShowCGIVariables extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String[][] variables = { { "AUTH_TYPE", request.getAuthType() }, { "CONTENT_LENGTH", String.valueOf(request.getContentLength()) }, { "CONTENT_TYPE", request.getContentType() }, Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com. Servlet and JSP training courses by book’s author: courses.coreservlets.com. 119