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

R

RC

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!
 
T

Thomas Weidenfeller

RC said:
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
 
R

RC

Thomas said:
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).
 
R

RC

Tor said:
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!
 
T

Tor Iver Wilhelmsen

RC said:
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().
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top