在servlet中调用方法

时间:2015-02-22 10:35:23

标签: java servlets

Predicts.java

public class Predicts {
    public void predict() throws Exception {
    }
}

Output.java

public class Output extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    }
}

在另一个servlet中,如何从Predicts.java初始化方法并调用它?

2 个答案:

答案 0 :(得分:1)

从Servlet类创建一个对象并调用方法:

public class Output extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

     Predicts p = new Predicts();
     p.predict();
}
}

答案 1 :(得分:-2)

this.predict();

无需创建新实例