0

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

1 Answer 1

2

I recommend works with Jython

Jython is an implementation of the Python programming language in Java, running on the Java Virtual Machine (JVM), as opposed to the standard implementation of Python which is implemented in C (CPython). Python developers can use it to take advantage of the vast libraries available to the Java community, which means adding a bit of Java integration in the application.

An example here

import org.python.util.PythonInterpreter;
import org.python.core.*;

public class SimpleExample {
   public static void main(String[] args) throws PyException
   {
       PythonInterpreter pi = new PythonInterpreter();
       pi.set("integer", new PyInteger(42));
       pi.exec("square = integer*integer");
       PyInteger square = (PyInteger)pi.get("square");
       System.out.println(“square: ” + square.asInt());
   }
}

source Embedding Jython in Java Applications

Sign up to request clarification or add additional context in comments.

3 Comments

I have to execute in servlet doPost() method, not in a simple class which has the main method
You can use the jython interpreter or command executor into your servlet.
i just want to run a python script from my servlet and primt the output, can u provide a code for that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.