[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

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?
0
prain
Asked:
prain
  • 4
  • 4
  • 4
2 Solutions
 
rrzCommented:
You could create a load-on-startup servlet. In its init method  you could set the ServletContext into a field of a
utility class.  In your web app's web.xml file  you could have something like
    <servlet>      
                   <servlet-name>AppContext</servlet-name>
                   <servlet-class>test.AppContextServlet</servlet-class>
                   <load-on-startup/>
    </servlet>  

Open in new window

and the servlet could  be something like
package test;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AppContextServlet extends HttpServlet {
    public void init(ServletConfig config){
                   try{
                       super.init(config);
                       AppContextHolder.set(config.getServletContext());
                       System.out.println("init of AppContextServlet");
                      }catch(ServletException se){System.out.println("error in AppContextSrevlet init");}
    }
}

Open in new window

and the utility class could be something  like  
package test;
import javax.servlet.ServletContext;
public class AppContextHolder{
   private static ServletContext currentContext;
   public static void set(ServletContext context) {
                                                   currentContext = context;
   }
   public static ServletContext getContext(){
                                             return currentContext;
   }
}

Open in new window

  Now with that code in place, you can use  
ServletContext application = AppContextHolder.getContext();
String value1 = application.getInitParameter("Param1");

Open in new window

in your POJO
   
0
 
mrcoffee365Commented:
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.
0
 
rrzCommented:
@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 ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
prainAuthor Commented:
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.getInitParameter("Param1");
   why "Param1" ? should'nt that be "param_name" ?  (if you please look at mt original web.xml?)

thanks for the input.

prain

 
0
 
mrcoffee365Commented:
>>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.getContext() 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.
0
 
prainAuthor Commented:
AppContextServlet.getContext()

AppContextServlet does not have a getContext().

only the AppContextHolder has getContext().
0
 
prainAuthor Commented:
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.getInitParameter("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.getContext();
String value1 = application.getInitParameter("Param1");

Considering that these two lines are in a complete outside POJO.

0
 
mrcoffee365Commented:
>>AppContextServlet does not have a getContext()

Yes -- if you look closely at the code rrz provided, the call is right there.

>>And also I cannot agree with "Parm1".  "Param1"

Yes, you clearly don't understand how context parameters work in web.xml.  They get a name, and a value.  In this fragment, which you posted:

<context-param>
      <param_name>Param1</param_name>
      <param-value>The actual value</param-value>
      <description> Again, some description </description>
    </context-param>

The name is "Param1" and the value is "The actual value".  Try a test .jsp program to get a context parameter, and you'll see how it works.
0
 
prainAuthor Commented:
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.getContext();
String value1 = application.getInitParameter("Param1");

The above two lines are put into a POJO like this

Class MyTest{
public void testWebXMlreader()
{
ServletContext application = AppContextHolder.getContext();
String value1 = application.getInitParameter("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

0
 
mrcoffee365Commented:
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.AppContextServlet;

at the top of your POJO code.
0
 
rrzCommented:
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(config.getServletContext());  
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.getContext();  

>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.getContext();
String value1 = application.getInitParameter("Param1");
}
}

Or alternatively, as mrcoffee said, use import statement.
>import com.mycompany.myapp.AppContextServlet;
No! not the Servlet .  
import com.mycompany.myapp.AppContextHolder;
0
 
rrzCommented:
>and "Again, some description" into three variables in my non HttpServlet class?  
I don't know how to get the description.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now