开发者

Glassfish Custom Properties Resource Not Loading From File

I've deployed a webapp (war) to Glassfish v3 and I am trying to get it to read from properties defined in a custom resource.

In my app, I've defined the properties as:

@Resource(mappedName = "TestServletProperties")
private Properties properties;

and make use of it like this:

protected void doGet(final HttpServletRequest request,
        final HttpServletResponse response) throws ServletException,
        java.io.IOException
{
    String propertyOne = properties.getProperty("testServlet.propertyOne");
    String propertyTwo = properties.getProperty("propertyTwo");

    StringBuffer buffer = new StringBuffer("Properties Retrieved\n");
    buffer.append("Property One: " + propertyOne + "\n");
    buffer.append("Property Two: " + propertyTwo + "\n");

    try
    {
        response.getWriter().write(buffer.toString());
    }

    catch (Exception ex)
    {
        try
        {
            log.warn("Exception thrown", ex);
            response.getWriter().write(ex.getStackTrace().toString());
        }
        catch (IOException io)
        {
            log.warn("IOException thrown", io);
        }
    }            
}

In Glassfish, I've created a JNDI Custom Resource called TestServletProperties of type java.util.Properties and using factory class org.glassfish.resources.custom.factory.PropertiesFactory. In the resource there is one property "fileName" with its value set to the absolute path of the properties file:

/Program Files/glassfishv3/glass开发者_高级运维fish/domains/domain1/applications/Test/WEB-INF/classes/TestServlet_lab.properties

I've also tried

c:\Program Files\glassfishv3\glassfish\domains\domain1\applications\Test\WEB-INF\classes\TestServlet_lab.properties

I have confirmed that the file exists and contains the referenced properties. Unfortunately, I'm getting back "null" for both values in my response.

Any thoughts?


The solution is that you have to use the fully qualified "org.glassfish.resources.custom.factory.PropertiesFactory.fileName" versus just "fileName".


The reason might be that you have a web.xml file with the header of a 2.4 (or older) servlet version.

@Resource and other annotations are only processed if you have at least version 2.5 in the header of web.xml. Be sure that you do not simply change the version but copy and paste the new header from somewhere as the namespace is different.

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜