internet.com logo The Java Boutique Java is a trademark of Sun Microsystems, Inc.
 APPLETS
by Category
by Date
by Name
Hall of Fame
Archive
Submit
Source Code
Servlets and JSP
 ARTICLES
Tutorials
Reviews
Glossary
 FORUM
FAQ
Users Poll
Discussion Group
Contact Us
 OTHER RESOURCES
Custom Applets
Java@Work
Java News
Jini Watch
Affiliate Programs
Top Searches
chat - counter
menu - countdown

internet.com
Internet News
Internet Stocks/VC
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International

Search internet.com
Advertising Info
Corporate Info

internet.commerce
Be a Commerce Partner
Free Conf. Calling
Advertise a Coupon
Hosted site services
Add a Career Center
Small Biz Purchasing
Get Targeted Traffic
get e-biz answers
Find a Web Host
PocketPC/Wireless Dev
Software Specials

WebDeveloper Network
Developer Forums
Developer News
ExtremeFlash
FlashKit
FlashPlanet
JavaBoutique
JavaScriptSource
ScriptSearch
SearchEngineWatch
StreamingMediaWorld
The WDVL
WebDeveloper.com
WebDevelopersJournal WebReference.com
The XML Files

Silicon Alley Jobs: The best place to find Internet jobs in the New York area.

Tutorials : Server-Side Web Applications Using Servlets and JSP :
Accessing the Standard CGI Variables :

Contents
Introduction to CGI Variables
Servlet Equivalent of Standard CGI Variables
Example: Reading the CGI Variables

3. Example: Reading the CGI Variables

Here's 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 shown in the previous section.

3.1 ShowCGIVariables.java

You can also download the source or try it on-line. Note: also uses ServletUtilities.java, shown earlier.
package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

/** Creates a table showing the values of all the CGI variables.
 *  

* Part of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ * 1999 Marty Hall; may be freely used or adapted. */ 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() }, { "DOCUMENT_ROOT", getServletContext().getRealPath("/") }, { "PATH_INFO", request.getPathInfo() }, { "PATH_TRANSLATED", request.getPathTranslated() }, { "QUERY_STRING", request.getQueryString() }, { "REMOTE_ADDR", request.getRemoteAddr() }, { "REMOTE_HOST", request.getRemoteHost() }, { "REMOTE_USER", request.getRemoteUser() }, { "REQUEST_METHOD", request.getMethod() }, { "SCRIPT_NAME", request.getServletPath() }, { "SERVER_NAME", request.getServerName() }, { "SERVER_PORT", String.valueOf(request.getServerPort()) }, { "SERVER_PROTOCOL", request.getProtocol() }, { "SERVER_SOFTWARE", getServletContext().getServerInfo() } }; String title = "Servlet Example: Showing CGI Variables"; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=CENTER>" + title + "</H1>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + "<TH>CGI Variable Name<TH>Value"); for(int i=0; i<variables.length; i++) { String varName = variables[i][0]; String varValue = variables[i][1]; if (varValue == null) varValue = "<I>Not specified</I>"; out.println("<TR><TD>" + varName + "<TD>" + varValue); } out.println("</TABLE></BODY></HTML>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

3.2 ShowCGIVariables Output

ShowCGIVariables: Output


Order CSAJSP
Order now

This page is part of my Tutorial on Servlets and JSP. © 1999 Marty Hall. All source code freely available for unrestricted use.

This tutorial was a very early outline of the material that eventually became Core Servlets and JavaServer Pages (Sun Microsystems Press and Prentice Hall, May 2000). See http://www.coreservlets.com/ for the topics covered, table of contents, index, reader reviews, and complete source code archive.


This tutorial is now available as a book: Core Servlets and JavaServer Pages by Marty Hall, published by Sun Microsystems Press. Read all about it at CoreServlets.com


Server-Side Web Applications using Java Servlets versions 2.1/2.2 and JavaServer Pages (JSP) version 1.0: A Tutorial
© 1999-2000 Marty Hall.
All source code freely available for unrestricted use.
Created for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and on-site Java short courses.
Please note that this is a first draft of the tutorial, so please send corrections, comments, and suggestions to me at hall@apl.jhu.edu.
Reprinted with permission from the author. Click here to visit the original version


Applet Index
(sorted alphabetically)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z #s
The Java Source
(applets w/source code)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z

Find out about becoming a Java Boutique sponsor.

Copyright 1999-2000 internet.com Corp.
All Rights Reserved. Legal Notices. Privacy Policy.