How can I read context parameter/web.xml values in a non-servlet java file?
Asked Answered
R

5

19

I've got a regular java file that I use to update and query a mysql database but I need to take configurable options in that file (like host name, password, etc) and put it in the web.xml file (or perhaps another file if that's an option, but ideally in web.xml).

But I don't know how to get access to web.xml values from a regular non-servlet java file.

Or would I need to read the xml (like any other xml file... or is there a shortcut route to this...)

Renteria answered 16/11, 2010 at 15:21 Comment(2)
You had really to mention that this "regular non-servlet Java file" is instantiated and executed in ServletContextListener#contextInitialized() (as per your previous question). That would make a lot difference in the correct answer...Shrum
What server are you using? Tomcat, JBoss, ???. Depending on the server params can be set at the server instance level in several ways.Inessa
I
33

You need to put the required parameters in env-entry entries of your web.xml file:

<env-entry> 
    <env-entry-name>dbhost</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>localhost</env-entry-value> 
</env-entry>

and then access them via the jndi context

import javax.naming.Context;
import javax.naming.InitialContext;
...
// Get the base naming context
Context env = (Context)new InitialContext().lookup("java:comp/env");

// Get a single value
String dbhost = (String)env.lookup("dbhost");
Inessa answered 16/11, 2010 at 15:32 Comment(4)
Be sure to specify env-entry-type before the env-entry-value, or you might get XML validation errors.Caboose
Maybe you can add the imports. :)Consort
@Consort there you goInessa
can your solution be achieved using annotations?Euphorbiaceous
S
11

You could use context-parameters in your web.xml and a javax.servlet.ServletContextListener to populate some static fields.

In you normal java class you read this this static fields.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
...
<context-param>
    <description>Prameter</description>
    <param-name>myParam</param-name>
    <param-value>123456790</param-value>
</context-param>
...
</web-app>

You can access this context parameter with ServletContext.getInitParameter

Swarthy answered 16/11, 2010 at 15:33 Comment(0)
K
3

One way is to read xml file and parse it.

You can put it on some static map in after parsing in ServletContextListener

Kynthia answered 16/11, 2010 at 15:26 Comment(0)
S
1

Implement a ServletContextListener:

package util;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyConfigListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext ctx = sce.getServletContext();

        String hostname = ctx.getInitParameter("my.config.hostname");

        // now go and do something with that
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {}

}

And don't forget to register it in web.xml:

<context-param>
  <param-value>somewhere.example.org</param-value>
  <param-name>my.config.hostname</param-name>
</context-param>
<listener>
  <listener-class>util.MyConfigListener</listener-class>
</listener>
Samira answered 8/6, 2016 at 8:38 Comment(0)
E
0

Create a static class that would be initialized from one of the servlets init.

Euxenite answered 16/11, 2010 at 15:36 Comment(1)
The difficulty here is in which servlet's init. You don't want your application to depend on when Servlet is initialized first.Johan

© 2022 - 2024 — McMap. All rights reserved.