head	1.12;
access;
symbols;
locks
	nobody:1.12; strict;
comment	@# @;


1.12
date	99.05.18.17.47.52;	author nobody;	state Exp;
branches;
next	1.11;

1.11
date	99.05.05.22.34.26;	author nobody;	state Exp;
branches;
next	1.10;

1.10
date	99.04.21.18.27.45;	author nobody;	state Exp;
branches;
next	1.9;

1.9
date	99.04.21.18.27.41;	author nobody;	state Exp;
branches;
next	1.8;

1.8
date	99.04.21.18.21.43;	author nobody;	state Exp;
branches;
next	1.7;

1.7
date	99.04.21.17.00.14;	author nobody;	state Exp;
branches;
next	1.6;

1.6
date	99.04.21.00.19.34;	author nobody;	state Exp;
branches;
next	1.5;

1.5
date	99.04.21.00.18.03;	author nobody;	state Exp;
branches;
next	1.4;

1.4
date	99.04.21.00.17.08;	author nobody;	state Exp;
branches;
next	1.3;

1.3
date	99.04.21.00.16.17;	author nobody;	state Exp;
branches;
next	1.2;

1.2
date	99.04.21.00.15.53;	author nobody;	state Exp;
branches;
next	1.1;

1.1
date	99.04.21.00.15.51;	author nobody;	state Exp;
branches;
next	;


desc
@null
@


1.12
log
@null
@
text
@IDependOn-Set: 1
IDependOn-Set: 118
IDependOn-Set: 119
IDependOn-Set: 140
IDependOn-Set: 171
IDependOn-Set: 2
LastModifiedSecs: 924719265
Parent: 119
SequenceNumber: 4
Title: How do I access environment variables from my servlet?
Part: 0
Author-Set: cmcclanahan@@mytownnet.com
LastModifiedSecs: 924653820
Type: monospaced
Lines: 15
Richard Yumul wrote:

> Can somebody please tell me how to access the server enviroment variables?  For
> example, how do you access variables like DOCUMENT_ROOT, etc.?
>
> Thanks,
> Rich Yumul
>

Most of the "interesting" CGI-related environment variables are directly
translated into methods in the HttpServletRequest.  For example, the
PATH_INFO value is returned by getPathInfo(), PATH_TRANSLATED by
getPathTranslated(), and so on.  The details are in the Javadoc API descriptions
of each method.

EndPart: 0
Part: 1
Author-Set: cmcclanahan@@mytownnet.com
LastModifiedSecs: 924653880
Type: monospaced
Lines: 5

The particular case of DOCUMENT_ROOT is interesting, because it is an Apache
extension, not a standard CGI variable (and thus it is not visible through the
standard servlet API).  Apache JServ does know about this value, however, and uses
it as the basis for paths calculated by the getRealPath() call.
EndPart: 1
Part: 2
Author-Set: muscarella@@stanford.edu
LastModifiedSecs: 924714000
Type: monospaced
Lines: 204
Actually, you can get other non-standard cgi-environment
variables in Apache-Jserv (and other servlet engines)
but unfortunatly it is not consistant accross 
Servlet Engines.  You do this using the getAttribute()
method along with the servlet specific attribute name.
In the case of Apache-Jserv it's "org.apache.jserv." but
you'd have to check on what to use for other servlet engines.
And just to mix things up more others make the variable
available via getHeaders() (JRUN for instance).


So, to get the DOCUMENT_ROOT variable you would do someting like

String DocRoot = getAttribute(org.apache.jserv.DOCUMENT_ROOT);

Below is a modification of Sun's SnoopServlet that will return
all available Apache-Jserv attributes.



// SuperSnoop based on Sun's SnoopServlet

import java.io.*;
import java.util.*;

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


/**
 * Snoop servlet. This servlet simply echos back the request line and
 * headers that were sent by the client, plus any HTTPS information
 * which is accessible.
 *
 * @@version     1.19 98/02/25
 * @@author      David Connelly
 */
public
class SuperSnoop extends HttpServlet {


    public void doPost (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        //value chosen to limit denial of service
        if (req.getContentLength() > 8*1024) {  
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();

            out.println("<html><head><title>Too big</title></head>");
            out.println("<body><h1>Error - content length &gt;8k not ");
            out.println("</h1></body></html>");
        } else {
            doGet(req, res);
        }
    }
    
    public void doGet (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        out.println("<html>");
        out.println("<head><title>Snoop Servlet</title></head>");
        out.println("<body>");

        out.println("<h1>Requested URL:</h1>");
        out.println("<pre>");
        out.println (HttpUtils.getRequestURL (req).toString ());
        out.println("</pre>");

        Enumeration enum = getServletConfig().getInitParameterNames();
        if (enum != null) {
            boolean first = true;
            while (enum.hasMoreElements()) {
                if (first) {
                    out.println("<h1>Init Parameters</h1>");
                    out.println("<pre>");
                    first = false;
                }
                String param = (String) enum.nextElement();
                out.println(" "+param+": "+getInitParameter(param));
            }
            out.println("</pre>");
        }

        out.println("<h1>Request information:</h1>");
        print(out, "<pre><P>Request method", req.getMethod());
        print(out, "Request URI", req.getRequestURI());
        print(out, "Request protocol", req.getProtocol());
        print(out, "Servlet path", req.getServletPath());
        print(out, "Path info", req.getPathInfo());
        print(out, "Path translated", req.getPathTranslated());
        print(out, "Query string", req.getQueryString());
        print(out, "Content length", req.getContentLength());
        print(out, "Content type", req.getContentType());
        print(out, "Server name", req.getServerName());
        print(out, "Server port", req.getServerPort());
        print(out, "Remote user", req.getRemoteUser());
        print(out, "Remote address", req.getRemoteAddr());
        print(out, "Remote host", req.getRemoteHost());
        print(out, "Authorization scheme", req.getAuthType());
        out.println("</pre>");
        
        Enumeration e = req.getHeaderNames();
        if (e.hasMoreElements()) {
            out.println("<h1>Request headers:</h1>");
            out.println("<pre>");
            while (e.hasMoreElements()) {
                String name = (String)e.nextElement();
                out.println(" " + name + ": " + req.getHeader(name));
            }
            out.println("</pre>");
        }

        e = req.getParameterNames();
        if (e.hasMoreElements()) {
            out.println("<h1>Servlet parameters (Single Value style):</h1>");
            out.println("<pre>");
            while (e.hasMoreElements()) {
                String name = (String)e.nextElement();
                out.println(" " + name + " = " + req.getParameter(name));
            }
            out.println("</pre>");
        }

        e = req.getParameterNames();
        if (e.hasMoreElements()) {
            out.println("<h1>Servlet parameters (Multiple Value style):</h1>");
            out.println("<pre>");
            while (e.hasMoreElements()) {
                String name = (String)e.nextElement();
                String vals[] = (String []) req.getParameterValues(name);
                if (vals != null) {
                    out.print("<b> " + name + " = </b>"); 
                    out.println(vals[0]);
                    for (int i = 1; i<vals.length; i++)
                        out.println("           " + vals[i]);
                }
                out.println("<p>");
            }
            out.println("</pre>");
        }
        

         // Attributes available from Jserv

      String prefix = "org.apache.jserv.";
      Object attrsObj = req.getAttribute("org.apache.jserv.attribute_names");
      if ( attrsObj != null && attrsObj instanceof Enumeration ) {
          Enumeration attrs = (Enumeration) attrsObj;
          out.println("<p>");
          out.println("<h1>Attributes</h1>");
          out.println("available via HttpServletRequest.getAttribute()");
          out.println("<br>");
          while ( attrs.hasMoreElements()) {
              String attr = attrs.nextElement().toString();
              if ( req.getAttribute(prefix + attr) != null ) {
                  out.println( prefix + attr + " = " +
                      req.getAttribute(prefix + attr).toString());
              } else {
                  out.println( prefix + attr + " = NULL " );
              }
              out.println("<br>");
          }
      }

        String  charset = res.getCharacterEncoding ();

        out.println ("<h1>Response Information:</h1>");
        out.println ("<pre>");
        out.println ("MIME character encoding: " + charset);
        out.println ("</pre>");

        out.println("</body></html>");
    }

    private void print(PrintWriter out, String name, String value)
        throws IOException
    {
        out.print(" " + name + ": ");
        out.println(value == null ? "&lt;none&gt;" : value);
    }

    private void print(PrintWriter out, String name, int value)
        throws IOException
    {
        out.print(" " + name + ": ");
        if (value == -1) {
            out.println("&lt;none&gt;");
        } else {
            out.println(value);
        }
    }

    public String getServletInfo() {
        return "A servlet that shows the request headers sent by the client";
    }
}


 
EndPart: 2
@


1.11
log
@null
@
text
@d5 1
@


1.10
log
@null
@
text
@d4 1
@


1.9
log
@null
@
text
@d3 1
d5 2
a6 2
LastModifiedSecs: 924714014
Parent: 2
@


1.8
log
@null
@
text
@a2 1
IDependOn-Set: 119
@


1.7
log
@null
@
text
@d43 1
a43 1
DateOfPart: 1999-Apr-21 10:00am
@


1.6
log
@null
@
text
@d5 1
a5 1
LastModifiedSecs: 924653883
d7 1
a7 1
SequenceNumber: 3
d41 210
@


1.5
log
@null
@
text
@a1 1
IDependOn-Set: 117
d3 1
d32 1
a32 1
DateOfPart: 1999-Apr-20  5:18pm
@


1.4
log
@null
@
text
@d5 1
a5 1
LastModifiedSecs: 924653827
d7 1
a7 1
SequenceNumber: 2
d11 1
a11 1
DateOfPart: 1999-Apr-20  5:17pm
d30 11
@


1.3
log
@null
@
text
@d5 1
a5 1
LastModifiedSecs: 924653776
d7 1
a7 1
SequenceNumber: 1
d9 21
@


1.2
log
@null
@
text
@d5 1
a5 1
LastModifiedSecs: 924653750
d7 2
a8 2
SequenceNumber: 0
Title: New Item
@


1.1
log
@null
@
text
@d2 1
@
