This is really not a Jess question, just a Web-app programming question. But I *can* tell you what's wrong, I think. In your BaseServlet, you're creating a Rete instance and storing it in the ServletContext; in your JSP, you're using "useBean" to create another, unrelated Rete instance in the HttpServletRequest (the "request" scope you use in useBean.) Changing that "request" to "application" would make the JSP use the Rete instance in the ServletContext; "application" scope refers to the attributes of the ServletContext itself. Whether this is architecturally the right thing to do is a question I'll leave up to you.

You can post general Java programming questions, and get excellent (and generally kind and polite) answers in the forums at JavaRanch -- see http://saloon.javaranch.com . I have been known to answer a Jess question there from time to time as well.


On Feb 7, 2007, at 10:31 AM, Antonino Lo Bue (gmail) wrote:

Hi,
I'm working for the first time with JSP & servlets, I've developed a servlet that uses Jess to import an ontology (from JessTab/Protege) and apply to
this some inference rules...

My problem is that the servlet run without errors printing out the facts in
the KB...but when I try to turn the servlet in JSP the servlet run but
without print out anything...

Antonino Lo Bue
ICAR-CNR research fellow
Palermo (Italy)


Here is code:

SERVLET IMPLEMENTATION (works fine):
__________________________

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import jess.*;
import se.liu.ida.JessTab.*;
import edu.stanford.smi.protege.*;
import edu.stanford.smi.protegex.owl.*;
import java.util.*;
public class servletHyperComp extends HttpServlet{


 public void doGet (HttpServletRequest request, HttpServletResponse
response)
 throws IOException, ServletException, FileNotFoundException {

  //Richiama i servletContext dal file web.xml
  ServletContext servletContext = getServletContext();

  //Imposta la working dir dell'istanza di Protégé
  String protegeDir = servletContext.getInitParameter("protegedir");
  String tomcatDir = System.getProperty("catalina.home");
  System.setProperty("protege.dir", tomcatDir+protegeDir);

  response.setContentType("text/html");

  //Inizializza i file di input ed il motore Rete
  String rulesFile = servletContext.getInitParameter("rulesfile");
String functionsFile = servletContext.getInitParameter ("functionsfile");
  String xsltFile = servletContext.getInitParameter("xsltfile");
  String projectFile = servletContext.getInitParameter("projectfile");


 /*istanzia un solo engine da condividere
    if(servletContext.getAttribute("engine")==null) { */

  try{
    Rete engine= new Rete(this);
    engine.addUserpackage(new JessTabFunctions());

     if ( new File(projectFile).exists())
      engine.executeCommand("(load-project "  + projectFile +")");

    servletContext.setAttribute("engine", engine);
    PrintWriter out = response.getWriter();
    engine.addOutputRouter("page",out);

      // new Batch().batch(rulesFile, engine);
    String batch1;
          batch1="(batch "+rulesFile +")";
          String batch2;
          batch2="(batch "+functionsFile +")";
          String batch3;
          batch3="(batch "+xsltFile +")";
          engine.executeCommand(batch1);
          engine.executeCommand(batch2);
          engine.executeCommand(batch3);

    print("<html>", engine);
    print(" <head>", engine);
    print("  <title>Hello Nino from JESS!</title>", engine);
    print(" </head>", engine);
    print(" <body>", engine);
       print("  <h1>Hello Nino!</h1>", engine);
    print("   <p>", engine);

   //Stampa i fatti della lista
   for (Iterator e = engine.listFacts();
     e.hasNext();) {
                 Object t = e.next();
                 print(t.toString(), engine);
           }

   print("   </p>", engine);
   print(" </body>", engine);
   print("</html>", engine);
  }

  catch (Exception je){
   throw new ServletException(je);

   }
  return;
 }


 private void print(String message, Rete engine)
  throws JessException {
   engine.executeCommand("(printout page\""+message +"\"crlf)");
   }

 public void doPost(HttpServletRequest request, HttpServletResponse
response) throws IOException, ServletException{
  doGet(request, response);
 }
}


JSP IMPLEMENTATION:

BaseServlet class:
_____________


import jess.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public abstract class BaseServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse
response)
        throws IOException, ServletException {
        doGet(request, response);
    }

    protected void checkInitialized() throws ServletException {
        ServletContext servletContext = getServletContext();
String rulesFile = servletContext.getInitParameter ("rulesfile"); String factsFile = servletContext.getInitParameter ("factsfile");
        if (servletContext.getAttribute("engine") == null) {
            try {
                Rete engine = new Rete(this);
engine.executeCommand("(batch \"" + rulesFile + "\")");
                engine.reset();
                if (new File(factsFile).exists())
engine.executeCommand("(load-facts \"" + factsFile +
"\")");
                servletContext.setAttribute("engine", engine);
            } catch (Exception je) {
                throw new ServletException(je);
            }
        }
    }

    protected void dispatch(HttpServletRequest request,
                            HttpServletResponse response,
                            String page)
        throws IOException, ServletException {

        ServletContext servletContext = getServletContext();
        RequestDispatcher dispatcher =
            servletContext.getRequestDispatcher(page);
        dispatcher.forward(request, response);
    }

}


**********************
HyperContainer servlet class:
______________________

package hyperJessGen;



import javax.servlet.*;
import javax.servlet.http.*;
import jess.*;
import se.liu.ida.JessTab.*;
import edu.stanford.smi.protege.*;
import edu.stanford.smi.protegex.owl.*;
import java.io.*;
import java.util.Iterator;


public class HyperContainer extends BaseServlet{

 public void doGet (HttpServletRequest request, HttpServletResponse
response)
 throws IOException, ServletException  {
  //inizializza la servlet
     checkInitialized();
  try{
  //ottiene lo userId
  String userId = (String) request.getParameter("userId");
  if (userId == null || userId.length() == 0) {
   dispatch(request, response, "/index.html");
   return;}
  //cancella le precedenti sessioni e ne crea una nuova
  request.getSession().invalidate();
  HttpSession session = request.getSession();
  session.setAttribute("userId", userId);
  ServletContext servletContext = getServletContext();
  Rete engine = (Rete) servletContext.getAttribute("engine");
  request.setAttribute("engine", engine);
   } catch (Exception je) {
   throw new ServletException(je);
   }
  dispatch(request, response, "/hyperContainer.jsp");
 }

}

**************
hyperContainer.jsp :
______________

<HTML>
<%@ page
import="jess.*,java.util.*,se.liu.ida.JessTab.*,edu.stanford.smi.prote ge.*,e
du.stanford.smi.protegex.owl.*" %>
 <jsp:useBean id="engine" class="jess.Rete" scope="request"/>

 <head>
  <title>Results facts from HyperJessGen</title>
 </head>
 <body>
  <h1>Results facts from HyperJessGen</h1>
  <p>
  <% engine.addOutputRouter("page", out);
  for(Iterator e =engine.listFacts(); e.hasNext();){
   Object t =e.next();
   String fact = t.toString();
   engine.executeCommand("(printout page \""+fact+"\" crlf)");
   }
engine.executeCommand("(printout page " + "\"Hello World from Jess via
JSP & Servlet!\" crlf)");

  %>
        </p>

 </body>
</HTML>



--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify owner-jess- [EMAIL PROTECTED]
--------------------------------------------------------------------

---------------------------------------------------------
Ernest Friedman-Hill
Advanced Software Research          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550                 http://www.jessrules.com



--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to