1

I have a JSP web site, not Spring MVC, and it has a config file web.xml.

There are a couple of settings within the web.xml file that I'd like to get.

However, I want to access those settings from within a class sitting in my Source Packages folder.

I know I can pass the ServletContect from the JSP to the class but I want to avoid this and just access the web.xml file from my class.

Is this possible?

EDIT

I have been looking at javax.servlet thinking what I want was in there but if it is I can't see it.

griegs
  • 22,624
  • 33
  • 128
  • 205

5 Answers5

1

Using a javax.servlet.ServletContextListener implementation, that allows a singleton-like access to context:

package test.dummy;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;

public  class ContextConfiguration implements ServletContextListener {

  private static ContextConfiguration _instance;

  private ServletContext context = null;

  //This method is invoked when the Web Application
  //is ready to service requests
  public void contextInitialized(ServletContextEvent event) {
    this.context = event.getServletContext();

    //initialize the static reference _instance
     _instance=this;
  }

  /*This method is invoked when the Web Application has been removed 
  and is no longer able to accept requests
  */
  public void contextDestroyed(ServletContextEvent event) {
    this.context = null;

  }

  /* Provide a method to get the context values */
  public String getContextParameter(String key) {
     return this.context.getInitParameter(key);
  }

  //now, provide an static method to allow access from anywere on the code:
  public static ContextConfiguration getInstance() {
     return _instance;
  }
}

Set it up at web.xml:

<web-app>
<listener>
    <listener-class>
     test.dummy.ContextConfiguration
    </listener-class>
  </listener>
<servlet/>
<servlet-mapping/>
</web-app> 

And use it from anywhere at the code:

ContextConfiguration config=ContextConfiguration.getInstance();
String paramValue=config.getContextParameter("parameterKey");
Tomas Narros
  • 13,390
  • 2
  • 40
  • 56
0

I think this is very close to your description: link.

Basically, you want to read parameters from web.xml programatically, right?

Michael Mao
  • 9,878
  • 23
  • 75
  • 91
  • Close but not quite. The class I am in is not a servlet it's a plain old class file and it needs to access the web.xml file. I do not have doPost etc events – griegs Sep 29 '10 at 04:30
  • Well, I believe ServletContext belongs to the Servlet class, so I would do this: use a Servlet to access the parameters and then pass them to any other libaray class to do something else. Also, please consider security while trying access web.xml from plain old Java class :) – Michael Mao Sep 29 '10 at 04:41
  • OK I get that. Do you have some sample code that you could post that shows me how to call a servlet from my class and return two values from it? Kind of a Java newbie in case you missed it. :) – griegs Sep 29 '10 at 05:07
0

Hmmm... I am assuming that once your web app is up then you are not going to make any change in the web.xml....

Now what you can do is a create a servlet which loads on the startup and initialize a singleton class. You can use the following setting in your web.xml.

  <servlet>
    <description></description>
    <display-name>XMLStartUp</display-name>
    <servlet-name>XMLStartUp</servlet-name>
    <servlet-class>com.test.servlets.XMLStartUp</servlet-class>
    <init-param>
      <param-name>log4j-init-file</param-name>
      <param-value>WEB-INF/classes/log4j.properties</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>

In tomcat if you set load-on-startup value 0, then it means that while loading it has got the highest priority. now in the servlets init method read all the init-parameters like this and set it in your singleton class.

String dummy= getInitParameter("log4j-init-file");
Favonius
  • 13,959
  • 3
  • 55
  • 95
  • Hmmm, a singleton. Not my favourite implementation but one that would certainly work. – griegs Sep 29 '10 at 05:11
  • totally agree and i think i might hold it as a last option. – griegs Sep 29 '10 at 05:18
  • "Not my favourite implementation": Well, the proper way would be to pass the params into your class, but you do not want that. An alternative to a singleton could be a ThreadLocal (set up by a Filter), but that is not that much different, and would only work if your class is being called from the thread that handles the incoming request. – Thilo Sep 29 '10 at 05:48
0

This is not easily possible and may not be an elegent solution. The only option I can suggest is to have your configuration options i a xml or properties file and put it in your WEB-INF/classes directory so you can look it up using ClassLoader.getResource or ClassLoader.getResourceAsStream

I know it may be a duplication of the configuration, but IMO its the elegent way.

naikus
  • 24,302
  • 4
  • 42
  • 43
0

I really don't like classes reading from web.xml... Why do you need that? IMHO it would be easier, cleaner and by far much more manageable if you prepared a properties file and a manager class that reads from there.

SoulWanderer
  • 315
  • 3
  • 11