Java Code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import jess.*;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author jacob
 */
@WebServlet(name = "test", urlPatterns = {"/test"})
public class test extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        Rete engine = new Rete();
        engine.addOutputRouter("page", out);

        String a = request.getParameter("a");
        String b = request.getParameter("b");
        
        try {

           engine.batch("/home/jacob/Jess/limri.clp"); 

           engine.reset();
 
           engine.executeCommand("(assert (gender \"M\"))");
           
         //  Fact f = new Fact("result", engine);
          // f.setSlotValue("id", new Value("1"));
         //  f.setSlotValue("answer", new Value("1"));
         //  engine.assertFact(f);
           
           System.out.println(engine.run()); 


        } catch (JessException je) {
            System.out.print("An error occurred at line " + je.getLineNumber());
            System.out.println(" which looks like " + je.getProgramText ());
            System.out.println("Message: " + je.getMessage());

            throw new ServletException(je);  
           
        } 
 
    }
    
    private void print(String message, Rete engine)
        throws JessException {
       engine.executeCommand("(printout page \"" + message + "\" crlf)");
        
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

HTML Code

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery.js"></script>
    </head>
    <body>
        value1:<input type="text" id="a" />
        value2:<input type="text" id="b" />
        <button id='submit'>Submit</button>
        <div id='return'></div>
    <script>
        $(document).ready(function(){
            $("#submit").bind("click", function(){
                var a = $('#a').val();
                var b = $('#b').val();
                $.post("test", {a:a, b:b}, function(data){
                    $('#return').html(data)
                });
            });
        });
    </script>
    </body>  
</html>

JSP Code

<%-- 
    Document   : index
    Created on : Jun 8, 2011, 11:38:54 AM
    Author     : jacob
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello</h1>
        <jsp:include page="/test" /> 
    </body>
</html>

-- JacobLashley - 2011-06-15

Topic revision: r2 - 2011-06-15 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback