Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7734825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:20:50+00:00 2026-06-01T07:20:50+00:00

First, apologies for repeating a variation of an often asked question. However, I am

  • 0

First, apologies for repeating a variation of an often asked question. However, I am struggling to understand the best approach here. I have an application that uses Hibernate and Glassfish 3. Like others, I would like to be able to load Hibernate properties from outside the war file. The only solution that I have managed to get to work is the following. It loads the properties from the domains/domain1/config folder.

private Properties getLocalHibernateProperties() {
  Properties properties = new Properties();
  try {
    File pf = new File(System.getProperty("user.dir"), PROPERTIES_FILE_NAME);
    InputStream inStream = new FileInputStream(pf);
    properties.load(inStream);
  }
  catch (Exception e) {
    e.printStackTrace();
  }     
  return properties;
} 

However, I have also found and example How to read properties file placed outside war? that uses the ServletContext. Combining the various parts:

in web.xml <listener-class>foo.bar.startup.HibernatePropertiesLoader</listener-class>

public class HibernatePropertiesLoader implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event){
        ServletContext context = event.getServletContext();
        context.setAttribute("settings", new HibernatePropertiesReader(context));
    }
    public void contextDestroyed(ServletContextEvent event){}
}

public class HibernatePropertiesReader {
  ServletContext ctx = null;

  public HibernatePropertiesReader(ServletContext ctx) {
    this.ctx = ctx;
  }

  public Properties getLocalHibernateProperties() {
    Properties properties = new Properties();
    try {
      InputStream inStream = ctx.getResourceAsStream(PROPERTIES_FILE);
      properties.load(inStream);
    }
    catch (Exception e) {
      e.printStackTrace();
      }     
    return properties;
    }
}

I can understand that the second solution will search the CLASSPATH of the Servlet – (is this correct?) and is therefore more flexible than my original solution. However, I don’t understand how to use (access) the properties from my HibernateUtils class. I guess that this means that there is something fundamental missing in my understanding of ServletContext….

any assistance, advice, better solutions appreciated…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:20:52+00:00Added an answer on June 1, 2026 at 7:20 am

    The ServletContext.getResourceAsStream is typically used to load a file from the web app itself. For example, if you place your properties files as:

    <CONTAINER_ROOT>/webapps/<CONTEXT>/WEB-INF/conf/hibernate.properties
    

    Then you can load the file using

    ctx.getResourceAsStream("/WEB-INF/conf/hibernate.properties");
    

    The method can also be used to load “resources” from jar files but the first method is probably better for getting a configuration file. The JavaDocs are quite helpful in this case.

    ==UPDATE==

    I see your problem now is, how do you get the properties object out of the servlet context and into your Hibernate configuration. This depends on what your application layout is like. Are you using Spring?

    Assuming you are, a quick and dirty solution would be to have HibernateUitl implement the ServletContextAware interface (assuming HibernateUtil is constructed by Spring) and have code like the following

    public class HibernateUtil implements ServletContextAware {
    
    private ServletContext servletContext;
    
    ...
    
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
    
    private Properties getProperties() {
      return servletContext.getAttribute("settings").getLocalHibernateProperties();  
    }
    
    ...
    
    }
    

    This couples HibernateUtil to the Servlet API though, which may be undesirable. A slightly cleaner solution might be to create a HibernatePropertiesSource interface and then have a ServletContextHibernatePropertiesSource implementation which has the above methods and inject that into HibernateUtil.

    Again, this is all assuming you are using Spring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here,
I have a small doubt, so apologies first. I am creating an iPhone application
First off, please accept my apologies if this question is basic, I mainly have
this is my first time asking a question here so apologies if I am
First off - apologies if this or a similar question has been asked before.
First of all, my apologies for the title of this question, I don't have
First off, apologies for the length... I have a Host/Plugin application akin to MAF.
I searched first, so apologies if it's already been asked. I have a couple
First off, apologies if this question has been asked before but I couldn't find
First: apologies for the question title, I don't actually know what the problem is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.