Creating a Rule-enabled Non-SOA Java EE Application 9-15
Figure 9–15 Create HTTP Servlet Wizard - Welcome
7. Click Next.
This displays the Web Application page, as shown in Figure 9–16
.
Figure 9–16 Create HTTP Servlet Wizard - Web Application
8. Select Servlet 2.5\JSP 2.1 Java EE 1.5 and click Next.
This displays the Create HTTP Servlet - Step 1 of 3: Servlet Information page.
9.
Enter values in Create HTTP Servlet - Step 1 of 3: Servlet Information page, as follows, and as shown in
Figure 9–17 .
9-16 Oracle Fusion Middleware Users Guide for Oracle Business Rules
■
Class : GradesServlet
■
Package : com.example.grades
■
Generate Content Type : HTML
■
Generate Header Comments : unchecked
■
Implement Methods : service checked and all other checkboxes unchecked
Figure 9–17 Create HTTP Servlet Wizard - Step 1 of 3: Servlet Information
10. Click Next.
This displays the Create HTTP Servlet: Step 2 of 3: Mapping Information dialog as shown in
Figure 9–18 .
Creating a Rule-enabled Non-SOA Java EE Application 9-17
Figure 9–18 Create HTTP Servlet Wizard - Step 2 of 3: Mapping Information
11. Configure this dialog as follows:
■
Name : GradesServlet
■
URL Pattern : gradesservlet
12. Click Finish.
JDeveloper adds a Web Content folder to the project and creates a GradesServlet.java
file and opens the file in the editor as shown in Figure 9–19
.
9-18 Oracle Fusion Middleware Users Guide for Oracle Business Rules
Figure 9–19 Generated GradesServlet.java
13.
Replace the generated servlet with the source shown in Example 9–2
.
Example 9–2 Business Rules Using Servlet for Grades Application
package com.example.grades; import java.io.IOException;
import java.io.PrintWriter; import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import oracle.rules.rl.exceptions.RLException; import oracle.rules.sdk2.decisionpoint.DecisionPoint;
import oracle.rules.sdk2.decisionpoint.DecisionPointBuilder; import oracle.rules.sdk2.decisionpoint.DecisionPointInstance;
import oracle.rules.sdk2.exception.SDKException; import oracle.rules.sdk2.repository.DictionaryFQN;
public class GradesServlet extends HttpServlet { private static final String CONTENT_TYPE = texthtml;
private static final String DICT_PKG = com.example.grades; private static final String DICT_NAME = GradingRules;
private static final DictionaryFQN DICT_FQN = new DictionaryFQNDICT_PKG, DICT_NAME;
private static final String DF_NAME = GradesDecisionFunction;
Creating a Rule-enabled Non-SOA Java EE Application 9-19
private DecisionPoint m_decisionPoint = null; init in init public void initServletConfig config throws ServletException {
super.initconfig; try {
specifying the Decision Function and Dictionary FQN load the rules from the MDS repository.
m_decisionPoint = new DecisionPointBuilder .withDF_NAME
.withDICT_FQN .build;
} catch SDKException e { System.err.printlnFailed to build Decision Point: +
e.getMessage; throw new ServletExceptione;
} }
public void serviceHttpServletRequest request, HttpServletResponse response throws ServletException,
IOException { retrieve parameters
String name = request.getParametername; String strScore = request.getParametertestScore;
open output document StringBuilder doc = new StringBuilder;
addHeaderdoc; create TestScore object to assert
final TestScore testScore = new TestScore; testScore.setNamename;
try { testScore.setTestScoreInteger.parseIntstrScore;
} catch NumberFormatException e{ use default val } get DecisionPointInstance for invocation
DecisionPointInstance point = m_decisionPoint.getInstance; set input parameters
point.setInputsnew ArrayList {{ addtestScore; }}; invoke decision point and get result value
TestGrade testGrade = null; try {
invoke the decision point with our inputs ListObject result = point.invoke;
if result.size = 1{ errordoc, testScore.getName, bad result, null;
} decision function returns a single TestGrade object
testGrade = TestGraderesult.get0; } catch RLException e {
errordoc, testScore.getName, RLException occurred: , e; } catch SDKException e {
errordoc, testScore.getName, SDKException occurred, e; }
if testGrade = null{ create output table in document
openTabledoc; addRowdoc, testScore.getName, strScore, testGrade.getGrade;
9-20 Oracle Fusion Middleware Users Guide for Oracle Business Rules
closeTabledoc; }
addFooterdoc; write document
response.setContentTypeCONTENT_TYPE; PrintWriter out = response.getWriter;
out.printlndoc; out.close;
} public static void addHeaderStringBuilder doc {
doc.appendhtml; doc.appendheadtitleGradesServlettitlehead;
doc.appendbody; doc.appendh1Test Resultsh1;
} public static void addFooterStringBuilder doc {
doc.appendbodyhtml; }
public static void openTableStringBuilder doc { doc.appendtable border=\1\;
doc.appendtr; doc.appendthNameth;
doc.appendthScoreth; doc.appendthGradeth;
doc.appendtr; }
public static void closeTableStringBuilder doc { doc.appendtable;
} public static void addRowStringBuilder doc, String name, String score, Grade grade{
doc.appendtr; doc.appendtd+ name +td;
doc.appendtd+ score +td; doc.appendtd+ grade.value +td;
doc.appendtr; }
public static void errorStringBuilder doc, String name, String msg, Throwable t{ doc.appendtr;
doc.appendtd+ name +td; doc.appendtd colspan=2+ msg + + t +td;
doc.appendtr; }
}
Example 9–2 includes a Oracle Business Rules Decision Point, that uses an MDS
repository to access the dictionary. For more information, see Section 7.5, What You
Need to Know About Using Decision Point in a Production Environment .
When you add the Servlet shown in Example 9–2
, note the following:
■
In the init method the servlet uses the Rules SDK Decision Point API for Oracle Business Rules. For more information on using the Decision Point API, see
Chapter 7, Working with Rules SDK Decision Point API .
■
The DecisionPointBuilder requires arguments including a decision function name and, in a production environment a dictionary FQN to access a
dictionary in an MDS repository, as shown:
Creating a Rule-enabled Non-SOA Java EE Application 9-21
m_decisionPoint = new DecisionPointBuilder .withDF_NAME
.withDICT_FQN
For more information on using the Decision Point API, see Chapter 7, Working
with Rules SDK Decision Point API .
9.5 Adding an HTML Test Page for Grades Sample Application