Struts and Servlet Together

Hi Guys,

One question.

I have one Struts web application in Tomcat 5.5 using JDK 1.5. Another technology used is Drools Rules Engine.
Thus, I was thinking to load the Drools rules engine (which took around 5-6 seconds) while I run the Tomcat. So this means that I need to create a Servlet with the init() method in it. Well, so far so good.

Now, the questions starts to come out. If I store the rules engine in [MyWebName]/WEB-INF/classes/rules.xml, how can I try to load it? I thought that in init(), there is no way to open file, am I right? I do not want to hard code the path unless I have to. Any solution or better idea?

David
LVL 16
suprapto45Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
properties.load(getClass().getResourceAsStream("rules.xml"));
0
 
CEHJCommented:
Just allow the classloader to do it with getResource(AsStream)
0
 
suprapto45Author Commented:
Hi CEHJ,

Thx. Yeah I found that method as well but I have no luck trying it. Do you have any URL to show how to use it?

David
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
Show me how you'd load the rules file normally
0
 
CEHJCommented:
(showing the classes involved)
0
 
suprapto45Author Commented:
RuleBase excelRules;

InputStream is = new BufferedInputStream(new FileInputStream(properties.getProperty("excel.decisiontable.path")));
excelRules = DecisionTableLoader.loadFromInputStream(is);
0
 
suprapto45Author Commented:
so how to get the properties in init method()?

properties.load(new FileInputStream([here is problem]));

David
0
 
suprapto45Author Commented:
That seems promising....give 15 mins to test

David
0
 
suprapto45Author Commented:
Sorry...be back on 30 mins
0
 
suprapto45Author Commented:
Hi CEHJ,

Sorry for that. It creates NullPointerException.

                        Properties properties = new Properties();
                        
                        properties.load(getClass().getResourceAsStream("ApplicationResources.properties"));

Am I in the right track? I thought that it should work

David
0
 
suprapto45Author Commented:
java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Properties.java:365)
        at java.util.Properties.load(Properties.java:293)
0
 
CEHJCommented:
Make sure it's in classes directory. Depending on the situation, it may need to be

properties.load(getClass().getResourceAsStream("/ApplicationResources.properties"));
0
 
suprapto45Author Commented:
Ah, that is my mistake.

After google it, I found that it should contain "/"
properties.load(getClass().getResourceAsStream("/ApplicationResources.properties"));

It works now. Thanks CEHJ

David
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.