Javascript 如何在我的jsp页面中使用编译器的功能
我有一个Javascript 如何在我的jsp页面中使用编译器的功能,javascript,html,jsp,servlets,compilation,Javascript,Html,Jsp,Servlets,Compilation,我有一个textarea,用户将通过它编写代码,当他按下提交按钮时,文本将被编译并显示结果。 有人能提供我可以使用的API吗 更多详情: 我有一个由textarea组成的表单,用户可以在其中编写代码并提交。 在提交时,我想通过jsp页面编译这段代码,并返回编译器的输出消息 还有一件事我没有提交代码的文件,我只有字符串。 string code=request.getparameter(“textareaCode”) P>那么,有没有办法编译这个代码,用于任何一种语言C++、C++或java?
textarea
,用户将通过它编写代码,当他按下提交按钮时,文本将被编译并显示结果。
有人能提供我可以使用的API吗
更多详情:
我有一个由textarea
组成的表单,用户可以在其中编写代码并提交。
在提交时,我想通过jsp
页面编译这段代码,并返回编译器的输出消息
还有一件事我没有提交代码的文件,我只有字符串。string code=request.getparameter(“textareaCode”)代码>
<> P>那么,有没有办法编译这个代码,用于任何一种语言C++、C++或java?
是否有任何API可供我使用
如何通过jsp页面进行系统调用,以便编译提交的代码?您可以使用Runtime.getRuntime().exec(“命令”)执行命令行命令代码>
例如,如果要编译java文件,请尝试以下操作:
import java.io.*;
public class JavaRunCommand {
public static void main(String args[]) {
String s = null;
try {
// Logic to save textarea code into java file.
// then compile a java file "javac fileName.java" command
// using the Runtime exec method
Process p = Runtime.getRuntime().exec("javac fileName.java");
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);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
您可以使用JavaCompiler接口中提供的运行时java编译特性,该接口接受输入/输出流。
第一步。将文本区提交的内容转换为输入流。
第二步。JavaCompiler=ToolProvider.getSystemJavaCompiler();
第三步。通过提供inputstream(您在步骤1中创建的)作为一个参数来调用compiler.run方法。您可以通过输出流将编译的输出捕获为另一个参数。请参阅运行方法签名:int run(输入流输入、输出流输出、输出流错误、字符串…参数)。您不能只编译字符串。你必须编译整个文件。