1

i am trying to built a Prediciton-Servlet. But i am not able to load my previous built classifier into the servlet. This is my code:

public class SucessPrediction extends HttpServlet {
private static final long serialVersionUID = 1L;

public SucessPrediction() {
    super();
    // TODO Auto-generated constructor stub
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    // Get formula input
        ...
    // Read Classifier and predict Success

    ServletContext context = getServletContext();
    InputStream resourceContent = context.getResourceAsStream("/WEB-INF/J48.model");
    try {
        Classifier J48 = (Classifier) weka.core.SerializationHelper.read(resourceContent);
        if (klassifizierer != null) {out.println("KLASSIFIZIERER IS LOADED");}
        predictionResult = J48.classifyInstance(DATA.firstInstance());
        DATA.classAttribute().value((int) ergebnis);
    return PredicitonResult;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

The Problem is, that it works when i built a normal JAVA Application. But in my servlet it doesn t work. I think it has something to do with File Paths. I read also this hints: File path to resource in our war/WEB-INF folder? But whatever i do, i can not load my classifier and classify my data.

My System is an Eclipse IDE with a integrated Tomcat7 with OS Ubuntu 14.04

Thanks for your help!

Community
  • 1
  • 1
  • 1
    What is your exception/log? What is your error? – Atilla Ozgur Dec 06 '14 at 12:53
  • java.lang.NullPointerException at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125) Then i tried this version: InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("RESOURCES/J48.model"); Classifier klassifizierer = (Classifier) weka.core.SerializationHelper.read(inputStream);" But there i get this error java.io.IOException: Stream closed at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:151) at java.io.BufferedInputStream.fill(BufferedInputStream.java:235) No idea how to load this damn classifier... – SheldonCooper Dec 06 '14 at 14:57
  • I just look for a possiblity to load my J48.model and classify a dataset with it... – SheldonCooper Dec 06 '14 at 15:00

1 Answers1

1

Change your code so that you can load your classifier differently.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    // Get formula input
        ...
    // Read Classifier and predict Success

    try {
        Classifier J48 = getClassifier();
        if (klassifizierer != null) {out.println("KLASSIFIZIERER IS LOADED");}
        predictionResult = J48.classifyInstance(DATA.firstInstance());
        DATA.classAttribute().value((int) ergebnis);
    return PredicitonResult;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

This way you can try different load strategies.

First, Absolute path In your development machine try first following. put your model file to some directory and try to load it by absolute path.

// absolute path
private Classifier getClassifier() throws Exception
{
   return (Classifier) weka.core.SerializationHelper.read("C:\J48.model");
}

Second, Relative path put your model files a web app directory and try to load it by absolute path.

// relative path in web.app
private Classifier getClassifier() throws Exception
{
    String relativeWebPath = "/ModelFiles/J48.model";
    String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
    return (Classifier) weka.core.SerializationHelper.read(absoluteDiskPath);
}
Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69