prain
asked on
How to retrieve parameters from web.xml from a non HttpServlet class
Hello,
I have a supporting java POJO class that helps process some infor for a HttpServlet class.
If I have some environment parameters defined in the web.xml like this..
<context-param>
<param_name>Param1</param_ name>
<param-value>The actual value</param-value>
<description> Again, some description </description>
</context-param>
So how do I retrieve Param1, "The actual value" and "Again, some description" into three variables in my non HttpServlet class?
I have a supporting java POJO class that helps process some infor for a HttpServlet class.
If I have some environment parameters defined in the web.xml like this..
<context-param>
<param_name>Param1</param_
<param-value>The actual value</param-value>
<description> Again, some description </description>
</context-param>
So how do I retrieve Param1, "The actual value" and "Again, some description" into three variables in my non HttpServlet class?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Usually I pass the ServletContext to the class when I need the class to do something. Or create a special class which reads web.xml parameters and provides them to the classes. But I like rrz's approach, too.
@mrcoffee
>Usually I pass the ServletContext to the class
In that case, why not pass the param values ?
>Or create a special class which reads web.xml parameters
Are you suggesting that the class parse the web.xml file ? What with JAXP ?
>Usually I pass the ServletContext to the class
In that case, why not pass the param values ?
>Or create a special class which reads web.xml parameters
Are you suggesting that the class parse the web.xml file ? What with JAXP ?
ASKER
Hello rrz@871311,
Sorry I am coming back to this problem late. Anyways I have two unclear questions:
1. well I understand all your work except for how the outside POJO (your last two lines) would know the ServletContext that was filled by the AppContextServlet.
I think my POJO (user of XML params) class need to know the ref of the Servlet (AppContextServlet) that loaded the web.xml params. If so how does my POJO (user of xml params) know to get the ref to that AppContextServlet?. Can you please clear this out?
2. Also in your
String value1 = application.getInitParamet er("Param1 ");
why "Param1" ? should'nt that be "param_name" ? (if you please look at mt original web.xml?)
thanks for the input.
prain
Sorry I am coming back to this problem late. Anyways I have two unclear questions:
1. well I understand all your work except for how the outside POJO (your last two lines) would know the ServletContext that was filled by the AppContextServlet.
I think my POJO (user of XML params) class need to know the ref of the Servlet (AppContextServlet) that loaded the web.xml params. If so how does my POJO (user of xml params) know to get the ref to that AppContextServlet?. Can you please clear this out?
2. Also in your
String value1 = application.getInitParamet
why "Param1" ? should'nt that be "param_name" ? (if you please look at mt original web.xml?)
thanks for the input.
prain
>>1. well I understand all your work except for how the outside POJO (your last two lines) would know the ServletContext that was filled by the AppContextServlet.
AppContextServlet is the servlet rrz provided the code for. It is loaded by the webapp, and gets the context parameters at that time. So when your POJO calls AppContextServlet.getConte xt() it gets the values loaded when AppContentServlet was init'd. AppContextServlet class needs to be imported into your POJO -- is that what you didn't understand? Use the import statement.
>> why "Param1" ? should'nt that be "param_name" ? (if you please look at mt original web.xml?)
You explicitly named the parameter "Param1" in your question, which is why rrz used that.
AppContextServlet is the servlet rrz provided the code for. It is loaded by the webapp, and gets the context parameters at that time. So when your POJO calls AppContextServlet.getConte
>> why "Param1" ? should'nt that be "param_name" ? (if you please look at mt original web.xml?)
You explicitly named the parameter "Param1" in your question, which is why rrz used that.
ASKER
AppContextServlet.getConte xt()
AppContextServlet does not have a getContext().
only the AppContextHolder has getContext().
AppContextServlet does not have a getContext().
only the AppContextHolder has getContext().
ASKER
And also I cannot agree with "Parm1". "Param1" is the actual value of entry "param_name".
So the "Key" is "param_name" and value is "Param1". So I think the call to the method shold be
String value1 = application.getInitParamet er("param_ name");
Cottect me if I wrong.
well, I am still not convinced how you would access the ServletContext from totally outside POJO. A ful example combined to the one you have shown will be helpful. I am specially confuded with the two lines
ServletContext application = AppContextHolder.getContex t();
String value1 = application.getInitParamet er("Param1 ");
Considering that these two lines are in a complete outside POJO.
So the "Key" is "param_name" and value is "Param1". So I think the call to the method shold be
String value1 = application.getInitParamet
Cottect me if I wrong.
well, I am still not convinced how you would access the ServletContext from totally outside POJO. A ful example combined to the one you have shown will be helpful. I am specially confuded with the two lines
ServletContext application = AppContextHolder.getContex
String value1 = application.getInitParamet
Considering that these two lines are in a complete outside POJO.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK. The "Parm1" part I understand. Thank you for clarifying that.
OK now lets look at rrz's code. He says the last two lines are completely outside the Servlet class (AppContextServlet) . In fact AppContextHolder who is giving out the context via the getContext() is a totally independent class and an instance of that is getting "Filled" by the AppContexServlet. So far it is good.
So now truning into the two lines rrz wrote
ServletContext application = AppContextHolder.getContex t();
String value1 = application.getInitParamet er("Param1 ");
The above two lines are put into a POJO like this
Class MyTest{
public void testWebXMlreader()
{
ServletContext application = AppContextHolder.getContex t();
String value1 = application.getInitParamet er("Param1 ");
}
}
So now , how does this method (in class MyTest) know the AppContextHolder object that was filled by the AppContextServlet? There is no syntax error. But my guess is that I will return null as the Param1 vlaue ,as there is no connection between the MyTest and AppContextServlet at this moment instantiated by the TomCat container. I think something (missing link) is missing.
What I think is that a reference to AppContextHolder instance that WAS FILLED by the AppContextServlet must be passed to the client (MyTest) somehow to get this working.
JMO.
prain
OK now lets look at rrz's code. He says the last two lines are completely outside the Servlet class (AppContextServlet) . In fact AppContextHolder who is giving out the context via the getContext() is a totally independent class and an instance of that is getting "Filled" by the AppContexServlet. So far it is good.
So now truning into the two lines rrz wrote
ServletContext application = AppContextHolder.getContex
String value1 = application.getInitParamet
The above two lines are put into a POJO like this
Class MyTest{
public void testWebXMlreader()
{
ServletContext application = AppContextHolder.getContex
String value1 = application.getInitParamet
}
}
So now , how does this method (in class MyTest) know the AppContextHolder object that was filled by the AppContextServlet? There is no syntax error. But my guess is that I will return null as the Param1 vlaue ,as there is no connection between the MyTest and AppContextServlet at this moment instantiated by the TomCat container. I think something (missing link) is missing.
What I think is that a reference to AppContextHolder instance that WAS FILLED by the AppContextServlet must be passed to the client (MyTest) somehow to get this working.
JMO.
prain
You use the import statement. In Java, classes are imported.
So you put something like -- depending on the class path for the AppContextServlet:
import com.mycompany.myapp.AppCon textServle t;
at the top of your POJO code.
So you put something like -- depending on the class path for the AppContextServlet:
import com.mycompany.myapp.AppCon
at the top of your POJO code.
Thank you, mrcoffee, for helping explain my code.
>1.
Tomcat(or whatever web server you are using) will load the Servlet when it starts your web app. We told it to do by including
<load-on-startup/>
within the <servlet> element which we placed in the web app's web.xml file.
When the Servlet is loaded its init method is called by Tomcat. In its init method the Servlet will set the context into the AppContextHolder class using a static method
AppContextHolder.set(confi g.getServl etContext( ));
Your POJO doesn't need to know anything about the instance of AppContextServlet. Your POJO will ask the AppContextHolder for the ServletContext object.
ServletContext application = AppContextHolder.getContex t();
>In fact AppContextHolder who is giving out the context via the getContext() is a totally independent class and an instance of that is getting "Filled" by the AppContexServlet. So far it is good.
Yes, the Servlet sets the context into AppContextHolder. But there is no instance. It just uses a static method and the class object.
>as there is no connection between the MyTest and AppContextServlet
There doesn't need to be.
>What I think is that a reference to AppContextHolder instance that WAS FILLED by the AppContextServlet must be passed to the client (MyTest) somehow to get this working.
No instance needed, just uses a static method. The easiest way to make it work is to put the AppContextHolder class into the same package as your MyTest class.
package test; // same as AppContextHolder
Class MyTest{
public void testWebXMlreader()
{
ServletContext application = AppContextHolder.getContex t();
String value1 = application.getInitParamet er("Param1 ");
}
}
Or alternatively, as mrcoffee said, use import statement.
>import com.mycompany.myapp.AppCon textServle t;
No! not the Servlet .
import com.mycompany.myapp.AppCon textHolder ;
>1.
Tomcat(or whatever web server you are using) will load the Servlet when it starts your web app. We told it to do by including
<load-on-startup/>
within the <servlet> element which we placed in the web app's web.xml file.
When the Servlet is loaded its init method is called by Tomcat. In its init method the Servlet will set the context into the AppContextHolder class using a static method
AppContextHolder.set(confi
Your POJO doesn't need to know anything about the instance of AppContextServlet. Your POJO will ask the AppContextHolder for the ServletContext object.
ServletContext application = AppContextHolder.getContex
>In fact AppContextHolder who is giving out the context via the getContext() is a totally independent class and an instance of that is getting "Filled" by the AppContexServlet. So far it is good.
Yes, the Servlet sets the context into AppContextHolder. But there is no instance. It just uses a static method and the class object.
>as there is no connection between the MyTest and AppContextServlet
There doesn't need to be.
>What I think is that a reference to AppContextHolder instance that WAS FILLED by the AppContextServlet must be passed to the client (MyTest) somehow to get this working.
No instance needed, just uses a static method. The easiest way to make it work is to put the AppContextHolder class into the same package as your MyTest class.
package test; // same as AppContextHolder
Class MyTest{
public void testWebXMlreader()
{
ServletContext application = AppContextHolder.getContex
String value1 = application.getInitParamet
}
}
Or alternatively, as mrcoffee said, use import statement.
>import com.mycompany.myapp.AppCon
No! not the Servlet .
import com.mycompany.myapp.AppCon
>and "Again, some description" into three variables in my non HttpServlet class?
I don't know how to get the description.
I don't know how to get the description.