如何从Servlet

时间:2017-03-12 08:25:27

标签: java python jsp servlets web-applications

我在python中编写了一个脚本,从IMDB数据库中获取Top_25电影列表,我在JSP中创建了一个Web应用程序,点击按钮“列出电影”它应该调用python脚本生成所有top_25电影。我使用runtime.exec()命令来调用python文件,但是在输出中我什么也得不到,我得到的是空控制台。有人可以帮我解决这个问题我需要在点击按钮后在网络应用上获得输出,如果你知道任何更好的解决方案,那么从servlet调用python类也请提示。

JSP代码:

<body>
<form action= "FirstServlet">
<table>
<tr>
<th>list all movies</th>

<td><input type ="submit" value = "list movies" /></td>

</tr>
<tr>
<td><input type ="text" name = "moviename" /></td>
<td><input type ="button" value ="search" ></td>
</tr>
</table>
</form>
</body>

Servlet脚本:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    //String searchTerm= request.getParameter("searchTerm");  

      //callPython.myMethod();//Call to java class method
        Process p = Runtime.getRuntime().exec("python /Commutatus/movie_names.py");
        BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while((line = bfr.readLine()) != null) {
        // display each output line form python script
            PrintWriter out = response.getWriter();
            out.print(line);
        }

      //request.setAttribute("searchTerm",searchTerm);
      //request.getRequestDispatcher("index.jsp").forward(request, response);
}

0 个答案:

没有答案