Search code examples
javaservletstomcat7weka

How to load Classifier (Weka) in Servlet


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!


Solution

  • 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);
    }