0
votes

My question is how does one include ResourceBundles, in a Jetty project?

I have a webservice project named ServletEnvironment.
ServletEnvironment exports my service as an exploded war directory, containing:

[war directory]/WEB-INF/classes/{ properties files }
[war directory]/WEB-INF/web.xml

ServletEnvironment implements classes from my JettyWeb project.
JettyWeb starts a jetty server as described below:

Server server = new Server(port);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/test");
webapp.setResourceBase("[war directory]//");
HandlerList hlist = new HandlerList();
hlist.addHandler(webapp);
server.setHandler(hlist);
server.start();

Then lastly I have a Servlet project, Servlet is referenced in my ServletEnvironments web.xml as below:

<!-- Listener -->
  <listener>
    <listener-class>com.basicservlet.BasicListener</listener-class>
  </listener>
  <!-- Servlet -->
  <servlet>
    <servlet-name>basicServlet</servlet-name>
    <display-name>Default Basic Http Servlet</display-name>
    <servlet-class>com.basicservlet.BasicHttpServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- Default Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>basicServlet</servlet-name>
    <url-pattern>/servlet/</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>60</session-timeout>
    <!-- 60 minutes -->
  </session-config>

In my Servlet, I attempt:

ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);

It returns null.

In my JettyWeb, right after server.start() I attempt:

URL rs = this.getClass().getResource(propertiesDir);
System.out.println("Found: " + rs.toString());

It also returns null. Although I didn't believe that one would work.

Any help would be much appreciated please.

1
try with a folder $jetty.home/resources/yourbundle.properties - vels4j
With $jetty.home I'm assuming you mean the war directory? So in my case [war directory]/resources/bundle.properties - user1479897
Information provided about how you access these bundles is not clear. You have many classloaders in play here, the calls to this.getClass().getResource() and ResourceBundle.getBundle() should all occur from within the Servlet contexts and scopes (like a ServletContextListener or a Servlet.init or Servlet request). Attempts to access this information from outside of these scopes will result in null. - Joakim Erdfelt
Ok I'll try to be more clear. I'll try and edit my question. - user1479897

1 Answers

0
votes
Properties properties;
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");
properties = new Properties();
properties.load(inputStream);
String value = properties.getProperty(key);

This works in my case with embedded-jetty and application.properties is on classpath