You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chris Richmond <cr...@referentia.com> on 2008/05/29 04:19:36 UTC
accessing web.xml
Hello all,
I have created a web service using JAX-WS and deployed my .war and it runs
fine, but now I want to convert some hard-coded config values to read values
from the WEB-INF/web.xml file that was created/deployed.
I have addes something like the following to my web.xml:
<context-param>
<description>test context param</description>
<param-name>testParam</param-name>
<param-value>my value</param-value>
</context-param>
And I want to access it from any of my classes within my web service. Now I
realize I could access this using context related calls in a JSP page or
something, but seeing as how mine are regular classes that are not JSP pages
and simply have JAX-WS annotations, I was thinking I would have to use a
more genereric container approach to finding these values.
Perhaps I shouldn't be using <context-param> at all, but some other type of
entry in web.xml? In either case, I can't seem to figure out how to locate
and use an entry like that from within any class in my JAX-WS web service.
Perhaps my approach is entirely wrong on this? Am I approaching this in
the right way, and if so, how do I read values from web.xml and if not, what
should I be doing?
Thanks,
Chris
Re: accessing web.xml
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Chris,
Chris Richmond wrote:
| Well after I added that entry, I realized I don't have access to any
context
| information/classes since this is just a plain JAX-WS web service and
not a
| web application, so it is not using the JSP/JSR technologies.
You mean that you don't have access to the ServletRequest object? Okay,
then <context-param> isn't going to work for you. <env-entry> is the
answer (or perhaps using a properties file that you load yourself from
somewhere else, which might be more appropriate... you make the call).
| That is my
| understanding anyway. So my question is, if that is the case, and I
want to
| use those context-params, how *DO* I access it from my code.
I think you're out of luck, unless you want to hack some type of static
object that you can access from anywhere, and whose configuration is
loaded on webapp startup from the <context-param> settings.
| You mention env-entry as well, but I have not used JNDI and am not really
| sure what it even does. I have read about it and seen lots of applied
| examples to other app servers/stacks/toolkits. But I can't seem to
locate a
| resource that shows how to use JNDI in a tomcat container web service to
| access an env-entry in my WEB-INF/web.xml.
|
| Any pointers to the right resources or samples would be great, but if they
| don't exist, some advice is also greatly appreciated.
The best place to look is usually the source (of the information). I
started with the DTD for the 2.3 web.xml file (2.4 is XML Schema with
virtually no self-documentation, while the older DYD is full of goodies):
"
<!--
The env-entry element contains the declaration of a web application's
environment entry. The declaration consists of an optional
description, the name of the environment entry, and an optional
value. If a value is not specified, one must be supplied
during deployment.
- -->
<!ELEMENT env-entry (description?, env-entry-name, env-entry-value?,
env-entry-type)>
<!--
The env-entry-name element contains the name of a web applications's
environment entry. The name is a JNDI name relative to the
java:comp/env context. The name must be unique within a web application.
Example:
<env-entry-name>minAmount</env-entry-name>
Used in: env-entry
- -->
<!ELEMENT env-entry-name (#PCDATA)>
<!--
The env-entry-type element contains the fully-qualified Java type of
the environment entry value that is expected by the web application's
code.
The following are the legal values of env-entry-type:
~ java.lang.Boolean
~ java.lang.Byte
~ java.lang.Character
~ java.lang.String
~ java.lang.Short
~ java.lang.Integer
~ java.lang.Long
~ java.lang.Float
~ java.lang.Double
Used in: env-entry
- -->
<!ELEMENT env-entry-type (#PCDATA)>
<!--
The env-entry-value element contains the value of a web application's
environment entry. The value must be a String that is valid for the
constructor of the specified type that takes a single String
parameter, or for java.lang.Character, a single character.
Example:
<env-entry-value>100.00</env-entry-value>
Used in: env-entry
- -->
<!ELEMENT env-entry-value (#PCDATA)>
"
As you can see, the <env-entry-name> will be the key in the JNDI
context, but it will be rooted under the java:comp/env sub-tree. This
basically means that you should be able to access that object like this:
<env-entry>
~ <env-entry-name>myObjectKey</env-entry-name>
~ <env-entry-value>This is a string</env-entry-value>
</env-entry>
Context ctx = new InitialContext();
String myString = (String)ctx.lookup("java:comp/env/myObjectKey");
Obviously, if you have data of a different type, you'll want to use the
appropriate object types.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkg/EGkACgkQ9CaO5/Lv0PCQpgCglSIHSUMrnYqAjBJXH5FGOGSp
jFsAn0FxTSGQDGyCF7nikZgdzMQ4Js46
=oFSz
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: accessing web.xml
Posted by Chris Richmond <cr...@referentia.com>.
Christopher,
Well after I added that entry, I realized I don't have access to any context
information/classes since this is just a plain JAX-WS web service and not a
web application, so it is not using the JSP/JSR technologies. That is my
understanding anyway. So my question is, if that is the case, and I want to
use those context-params, how *DO* I access it from my code.
You mention env-entry as well, but I have not used JNDI and am not really
sure what it even does. I have read about it and seen lots of applied
examples to other app servers/stacks/toolkits. But I can't seem to locate a
resource that shows how to use JNDI in a tomcat container web service to
access an env-entry in my WEB-INF/web.xml.
Any pointers to the right resources or samples would be great, but if they
don't exist, some advice is also greatly appreciated.
Thanks,
Chris
-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net]
Sent: Thursday, May 29, 2008 5:15 AM
To: Tomcat Users List
Subject: Re: accessing web.xml
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Chris,
Chris Richmond wrote:
| I have created a web service using JAX-WS and deployed my .war and it runs
| fine, but now I want to convert some hard-coded config values to read
values
| from the WEB-INF/web.xml file that was created/deployed.
Sounds good.
| I have addes something like the following to my web.xml:
|
| <context-param>
| <description>test context param</description>
| <param-name>testParam</param-name>
| <param-value>my value</param-value>
| </context-param>
Rest assured... this is a reasonable choice for configuring your webapp.
There is a thread on this list right now where some folks are arguing
over how best to do this (see thread "Best practice? configuration of a
web application"). <context-params> in web.xml is as good as any method IMO.
| And I want to access it from any of my classes within my web service.
Okay. What have you tried? What container objects do you have access to?
Can you get to the request?
| Perhaps I shouldn't be using <context-param> at all, but some other
type of
| entry in web.xml? In either case, I can't seem to figure out how to
locate
| and use an entry like that from within any class in my JAX-WS web
service.
You could use <env-entry> instead, and then those settings would be
available via JNDI (which you can get to from anywhere) instead of
through the ServletContext (which you might not have available).
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkg+yGMACgkQ9CaO5/Lv0PArpQCglc5w9UVy9XcqOBzjZVeD5pvm
/ngAnijcF+3cMHQPsgsW8rXGQyghjxdT
=TBt0
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: accessing web.xml
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Chris,
Chris Richmond wrote:
| I have created a web service using JAX-WS and deployed my .war and it runs
| fine, but now I want to convert some hard-coded config values to read
values
| from the WEB-INF/web.xml file that was created/deployed.
Sounds good.
| I have addes something like the following to my web.xml:
|
| <context-param>
| <description>test context param</description>
| <param-name>testParam</param-name>
| <param-value>my value</param-value>
| </context-param>
Rest assured... this is a reasonable choice for configuring your webapp.
There is a thread on this list right now where some folks are arguing
over how best to do this (see thread "Best practice? configuration of a
web application"). <context-params> in web.xml is as good as any method IMO.
| And I want to access it from any of my classes within my web service.
Okay. What have you tried? What container objects do you have access to?
Can you get to the request?
| Perhaps I shouldn't be using <context-param> at all, but some other
type of
| entry in web.xml? In either case, I can't seem to figure out how to
locate
| and use an entry like that from within any class in my JAX-WS web
service.
You could use <env-entry> instead, and then those settings would be
available via JNDI (which you can get to from anywhere) instead of
through the ServletContext (which you might not have available).
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkg+yGMACgkQ9CaO5/Lv0PArpQCglc5w9UVy9XcqOBzjZVeD5pvm
/ngAnijcF+3cMHQPsgsW8rXGQyghjxdT
=TBt0
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org