At 03:28 PM 11/21/2003, you wrote:
I would be interested in seeing how to read an environment variable from the init method of a servlet.

Assuming you're asking about JVM environment variables since we're talking about the general case (not *nix or Win enviro variables). If so, you can define your param on the command line when you start Tomcat and access it like you do any other system variable:


System.getProperty("myApp.myProperty");

This is one way to do it, but it's not reasonable if you need any flexibility or room to grow. Instead, consider creating a properties file or xml configuration file. Load this file and read your parameters from it. I'll leave the details of that to the archives...

Hope this is what you're looking for.

justin


-----Original Message-----
From: Justin Ruthenbeck [mailto:[EMAIL PROTECTED]
Sent: Friday, November 21, 2003 2:54 PM
To: Tomcat Users List
Subject: RE: Server URL Before Request



[This is a general comment: I don't disagree with Chuck Goehring]

This is a common way to solve a problem such as the one Harris asked, but
in general I don't think using init parameters are a very reasonable
solution.  Here's why:

The web.xml defines the way in which the components of an application
interact.  It defines what urls are mapped to which resources, how errors
should be handled, and (unfortunately, IMHO) definitions of roles and
resource authorization mappings.  These are all properties of your web
application and say *nothing* about how the app is deployed.

Frequently, developers have situations where a webapp needs to be aware
of the environment in which it is running.  In this case, it's the name
and/or port that the app is running on.  In others, it's JNDI resource
lookup information.  In still others, it's application configuration
information that is long lived (how to configure management threads, for
example).

The point is, this is all information that belongs to a particular
*server*, not to the webapp.  It's different for every deployment because
the machine for every deployment is different.  You could always
configure this stuff in a <Context>, but that's Tomcat specific and is of
no help when you need to run on different platforms -- now or in the
future.

Instead, leave the deployment configuration parameters up to the person
responsible for deploying -- and observe the boundaries even if it's a
one-developer developed app.  Create a separate configuration mechanism
(xml on the classpath, properties file, command line args, whatever is
accessible on every platform) and use that to store this type of
information.

Granted, small projects can use the crutches of built-in nicities, but
for anyone developing a true J2EE app (not a Tomcat app), do yourself a
favor and maintain strict separation between build-time configuration
parameters and deploy-time ones.

justin

[Can you tell there's pent up frustration from having to deal with
this?]  :)


At 01:51 PM 11/21/2003, you wrote: >If you don't get a maintenance-free method from the group, you can >always do the following: > >In the servlet: > public void init() throws ServletException { > String lConnectTNSName = > getServletContext().getInitParameter("ConnectTNSName"); > if(lConnectTNSName == null) > throw new RuntimeException("ConnectTNSName is null"); >... > >In the web applications web.xml I store the site-specific info: ><web-app> > <display-name>**** (display-name)</display-name> > <description>*** servlets and JSP pages.</description> > <context-param> > <param-name>ConnectTNSName</param-name> > <param-value>xxxx</param-value> > </context-param> >... > >You could store any info you want this way and it only needs to be >re-entered when a change to your web.xml is required. > >Hope the group gives you a better answer, but this is handy for a >variety of things. > >Chuck > > > >-----Original Message----- >From: Harris Reynolds [mailto:[EMAIL PROTECTED] >Sent: Friday, November 21, 2003 12:29 PM >To: '[EMAIL PROTECTED]' >Subject: Server URL Before Request > > >Is there a mgmt API that will allow me to get the URL that the server is >running on from within the servlet init method (i.e. *before* the first >request comes in)? ...really all I would need is the port number (the >hostname and context paths would be easy enough to get). >thanks for any tips, >~harris > > >--------------------------------------------------------------------- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] > > >--------------------------------------------------------------------- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED]


______________________________________________ Justin Ruthenbeck Software Engineer, NextEngine Inc. justinr - AT - nextengine DOT com Confidential. See: http://www.nextengine.com/confidentiality.php ______________________________________________


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]


______________________________________________
Justin Ruthenbeck
Software Engineer, NextEngine Inc.
justinr - AT - nextengine DOT com
Confidential. See:
http://www.nextengine.com/confidentiality.php
______________________________________________


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to