0

I'm currently writing a Client / Server Web Application Using Java Servlets and Python. Although I'm having an issue with client side interactions, mainly in generating the input. Generating the input requires the data entered by the user to be modified by the execution of a python script locally on the client then submitted through a POST request to the Servlet Server. After which time the Servlet will then pass the received data to another python script server side to verify the validity of the input given. Python is necessary in the process due to computations being Massive (2^4096) bit length

So what I'm really asking is;

When a user visits a web page with a form, fills the form out then clicks the submit button, how can I extract the data out of the form fields, passing the content into my local python script as input parameters, execute the python script. Then return the new computed data from the python script back to the web browser for the POST request to the Server.

D3vRandom
  • 155
  • 1
  • 1
  • 12

1 Answers1

2

You have two possibilities here :

1) Java as a controller

From your Java Servlet, get the form data :

String Form_text_data = request.getParameter("text_input");

If there are many fields you can instantiate a StringBuilder (with all requested filed that you need) to get one string of all target field answer - be sure to make sanity check of those fields before adding them to your String Builder.

Then pass your String result from the string builder to the Runtime class which has an exec method :

StringBuilder Python_script_command = new StringBuilder("python PATH/TO/YOUR/PYTHON_SCRIPT.py");

Python_script_command.append(Form_text_data) // Repeat this as many time as you wish for each form fields, but be sure to make sanity check before to avoid any injections that you do not want

Process p = Runtime.getRuntime().exec(Python_script_command.toString());

You can read the output then by binding it in this way :

       BufferedReader stdInput = new BufferedReader(new 
             InputStreamReader(p.getInputStream()));

        BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(p.getErrorStream()));

    // read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);
    }

please see this article for more details :

http://alvinalexander.com/java/edu/pj/pj010016

2) Execute from a python API Framwork such as FLASK

You could also build a REST API directly in python and when the user submit the form you send it through a POST request to your python app

And then it is extremely simple to extract the data you need :

from flask import Flask, request
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    default_name = '0' 
    data = request.form.get('input_name', default_name) #here is an example on one data getter from a form field

see this answer for more details (How to get form data in Flask?)

then use this data directly in python, again be sur to make all kinds of sanity checks

jossefaz
  • 3,312
  • 4
  • 17
  • 40