Background
One of the big problems with technology nowadays is how much we use without fully understanding what is going on in the background – so the objective of these posts is to take you step by step through the technology with a better understanding of what is going on.
Overview
In this post we are going to create a very simple rule that is exposed via a Servlet.
The Steps – Part 1 – Servlet
Create a Maven Web Project
- Right click in Project Explorer
- New -> Other -> Maven -> Maven Project
- Next
- Next
- Select Archetype: org.apache.maven.archetypes :: maven-archetype-webapps
- Group id: com.skills421.examples.droolseip
- Artifact Id: ServletExample
- click Finish
Note the errors showing on the index.jsp file that is created
This is complaining that javax.servlet.HttpServlet is not found
Add HttpServlet to the Pom.xml
Search for javax.servlet on mvnrepository.com and determine the maven dependency for the latest release.
Edit your pom.xml and add the dependency. In my case that is the following:
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>servlet-api</artifactId> <version>6.0.41</version> </dependency>
Note, that this dependency will not be required by the server we choose to run on as javax.servlet is included in the server.
For this reason we need to change the scope of our dependency to compile. This tells the compiler that the dependency is only needed at compile time.
This gives:
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>servlet-api</artifactId> <version>6.0.41</version> <scope>compile</scope > </dependency>
Finally let’s add the build plug-in to tell Maven to use Java 1.6
<build> ... <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build>
Right click on your project and select Maven -> Update Project to force the project to update.
Run the Defaullt Project
Right click on your project and select Run As -> Run on Server
The project should display the default Hello World! page
Add src/main/java
You may find that the src/main/java folder has not been created for your project.
We can add this with the following simple steps:
- In Package Explorer, expand the folder src.
- Right click on the folder main and select new -> folder
- give the folder a name of java
- click finish
Add a RuleRunner Servlet
Now let’s create a RuleRunner Servlet as follows:
Right click on src/main/java
- New -> Other -> Web -> Servlet
- next
- Java package: com.skills421.servlets
- Class name: RuleRunner
- next
- note the URL mapping of /RuleRunner
- next
- finish
Add a few lines to the doGet method to test that our servlet is being called successfully as detailed in the code below:
package com.skills421.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class RuleRunner */ public class RuleRunner extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RuleRunner() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter writer = response.getWriter(); writer.println("doGet invoked OK"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
Run our Servlet
Re-run our project and use the web browser to navigate to: http://localhost:8080/ServletExample/RuleRunner
You should see one line displayed in the browser
doGet invoked OK
The Steps – Part 2 – Invoking Rules
Add Drools Dependencies to the pom.xml
Add the following dependencies to your pom.xml
<!-- Drools dependencies --> <dependency> <groupId>org.drools</groupId> <artifactId>knowledge-api</artifactId> <version>${drools.version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>${drools.version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>${drools.version}</version> </dependency>
and add the following properties section
<properties> <drools.version>5.6.0.Final</drools.version> <commons-version>1.1.3</commons-version> <junit.version>4.11</junit.version> </properties>
Now let’s add a very simple runRules method to our RuleRunner servlet as follows:
private void runRules(PrintWriter writer) { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(new ClassPathResource("rules/skills421/examples/rules.drl", getClass()),ResourceType.DRL); if (kbuilder.hasErrors()) { if (kbuilder.getErrors().size() > 0) { for (KnowledgeBuilderError kerror : kbuilder.getErrors()) { System.err.println(kerror); } } } KnowledgeBase kbase = kbuilder.newKnowledgeBase(); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ksession.setGlobal("writer", writer); ksession.fireAllRules(); }
Create the following folders in src/main/resources
- rules/skills421/examples
add the file rules.drl with the following content:
package rules.skills421.examples import java.io.PrintWriter; global PrintWriter writer; rule "always true" dialect "mvel" when then writer.println("Woohoo - Rules are running!"); end
Finally let’s modify our doGet() to invoke the Rules
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter writer = response.getWriter(); writer.println("doGet invoked OK"); runRules(writer); }
Run our Servlet
Re-run our project and use the web browser to navigate to: http://localhost:8080/ServletExample/RuleRunner
You should see one line displayed in the browser
doGet invoked OK
Woohoo – Rules are running!