I have written the following code for the servlet:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Summarize extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String str = req.getParameter("txt");
try {
//instead of writing just test.py, write the whole path to the python script it will work perfectly
Process p = Runtime.getRuntime().exec("python test.py");
p.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream()));
StringBuffer buf = new StringBuffer("");
String line = null;
System.out.println(p.toString());
while ((line = in.readLine()) != null) {
System.out.println(line);
buf.append(line);
}
System.out.println(buf.length());
out.println("this is summarize class");
} catch (Exception e) {
e.printStackTrace();
}
}
}
This code won't return anything but If I write the same code in a simple java class the code works perfectly fine.
I have also checked the python script, it's running perfectly fine.
Is there any way to fix this?
Update:
The following code will now work just read the comment just after the try statement