Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to get all httpd environment variables in JSP/Servlet?

Reply
Thread Tools

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

 
 
RC
Guest
Posts: n/a
 
      07-12-2005
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!
 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      07-12-2005
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/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
 
 
 
RC
Guest
Posts: n/a
 
      07-12-2005
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).
 
Reply With Quote
 
RC
Guest
Posts: n/a
 
      07-12-2005
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!
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      07-12-2005
RC <(E-Mail Removed)> 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().
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can I get a new httpd.conf file Myron Perl Misc 19 02-10-2010 05:53 PM
Website is loaded as "application/x-httpd-php" Matthias Pospiech HTML 2 06-05-2005 07:07 PM
Re: {Virus?} Mail Delivery (failure httpd@ncsa.uiuc.edu) httpd@ncsa.uiuc.edu Python 0 09-09-2004 06:53 AM
Re: Configure Apache HTTPD not to see the webapp name in the URL Ben Jessel Java 5 06-29-2004 02:59 AM
Apache HTTPD + Tomcat and random missing images Anders Skar Java 3 02-26-2004 04:25 PM



Advertisments