How to get all httpd environment variables in JSP/Servlet?

Discussion in 'Java' started by RC, Jul 12, 2005.

  1. RC

    RC Guest

    To get all httpd environment variables in CGI/Perl
    is very easy as below:

    print "Content-type: text/plain\n\n";

    if ($ENV{'REQUEST_METHOD'} eq "POST") {
    read (STDIN,$in,$ENV{'CONTENT_LENGTH'});
    print "$in\n";
    }

    foreach $var (sort(keys(%ENV))) {
    $val = $ENV{$var};
    $val =~ s|\n|\\n|g;
    $val =~ s|"|\\"|g;
    print "${var}=\"${val}\"\n";
    }


    But how to do that in JSP/Servlet?

    I know javax.servlet.http.HttpServletRequest
    has many getXXXX() methods. But it is not
    enought get all the environment variables.

    Does anyone out there know a/some method(s)
    in JSP/Servlet can get all httpd environment
    variables?

    Thank Q very much in advance!
     
    RC, Jul 12, 2005
    #1
    1. Advertising

  2. RC wrote:
    > To get all httpd environment variables in CGI/Perl
    > is very easy as below:
    >
    > print "Content-type: text/plain\n\n";
    >
    > if ($ENV{'REQUEST_METHOD'} eq "POST") {
    > read (STDIN,$in,$ENV{'CONTENT_LENGTH'});
    > print "$in\n";
    > }
    >
    > foreach $var (sort(keys(%ENV))) {
    > $val = $ENV{$var};
    > $val =~ s|\n|\\n|g;
    > $val =~ s|"|\\"|g;
    > print "${var}=\"${val}\"\n";
    > }
    >
    >
    > But how to do that in JSP/Servlet?


    I don't know how often you want to post the same question, but what
    about first of all getting the question right? There are no environment
    variables in HTTP (it is just that the CGI interface is handing over
    information in environment variables). But servlets don't follow the CGI
    interface, don't use environment variables, and don't care about them. I
    would suggest that you spend some time to get your terminology right,
    e.g. by studying the HTTP standard and a servlet textbook.

    > I know javax.servlet.http.HttpServletRequest
    > has many getXXXX() methods. But it is not
    > enought get all the environment variables.


    Of course not, because there are no environment variables in a HTTP
    request. You probably want the HTTP request headers, and the servlet API
    has appropriate methods to get them from an HTTP request.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Jul 12, 2005
    #2
    1. Advertising

  3. RC

    RC Guest

    Thomas Weidenfeller wrote:

    > There are no environment
    > variables in HTTP (it is just that the CGI interface is handing over
    > information in environment variables). But servlets don't follow the CGI
    > interface, don't use environment variables, and don't care about them.



    OK, then please give me some suggestions. The reason I want to find out
    the environment variables about HTTP is
    I am running Apache HTTP server and Tomcat.

    In the httpd.conf file I added two lines shows below

    setenv PRIMARY_DB_SERVER host1.domain
    setenv SECONDARY_DB_SERVER host2.domain

    So this is easy for me write CGI/Perl program to get
    the environment variables about HTTP.
    When the primary database server failed, will auto
    switch to secondary database server.

    Now I want to do the similar things by used JSP/Servlet.
    Since there is Tomcat don't use enviroment variables
    and don't care about them. Is there a way configurate
    Tomcat server? Of cource, I can hard code as

    try {
    connectToDB(host1.domain);
    } catch (Exception e) {
    try {
    connectToDB(host2.domain);
    } catch (Exception ee) {
    out.println("All db servers are failed" + ee);
    }
    }

    But I prefer do the similar way as I did in CGI, get the primary,
    secondary db servers from Tomcat configureation.

    P.S. One disadvantage about hard coding is when one day we decide move
    the database servers to host3.domain and host4.domain. Then I
    have to edit the codes and recompiling them. My boss will
    yelling to me after I come back from vacation.
    Therefore, I prefer before I go to vacation, tell my boss edit
    the config file(s) and restart the HTTP and/or Tomcat server(s).
     
    RC, Jul 12, 2005
    #3
  4. RC

    RC Guest

    Tor Iver Wilhelmsen wrote:

    >
    > You want to configure a particular application? You can set init
    > parameters in web.xml and get them via
    > getServletContext().getInitParameter().


    Hi Buddy, thank Q very much! Thumb up!
     
    RC, Jul 12, 2005
    #4
  5. RC <> writes:

    > Now I want to do the similar things by used JSP/Servlet.
    > Since there is Tomcat don't use enviroment variables
    > and don't care about them. Is there a way configurate
    > Tomcat server?


    You want to configure a particular application? You can set init
    parameters in web.xml and get them via
    getServletContext().getInitParameter().
     
    Tor Iver Wilhelmsen, Jul 12, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D. Alvarado
    Replies:
    3
    Views:
    5,262
    DebbieinFlorida
    Sep 17, 2008
  2. circuit_breaker
    Replies:
    2
    Views:
    2,061
    Jack Jia
    Apr 4, 2004
  3. javadev
    Replies:
    5
    Views:
    13,048
    javadev
    Nov 16, 2006
  4. Replies:
    4
    Views:
    827
  5. Myron

    can I get a new httpd.conf file

    Myron, Feb 8, 2010, in forum: Perl Misc
    Replies:
    19
    Views:
    305
    Steve C
    Feb 10, 2010
Loading...

Share This Page