如何在jsp中编译.java文件? [英] How can compile I `.java file` in jsp?

查看:98
本文介绍了如何在jsp中编译.java文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用java compiler API我想在jsp中编译java file.我创建了一个html文件,并使用其textArea元素将其文本(应为用户输入的Java代码)发送到服务器上的JSP,并将其收集为字符串后,制成了扩展名为.java的文件,并用textArea内容写了它,然后我使用了编译器API,但它没有在编译文件.我想做类似

Using java compiler API I want to compile a java file in jsp. I created a html file and using its textArea element I sent its text, which is expected to be code in java entered by user, to JSP on server and after collecting it in a string I made a file with .java extension and wrote it with textArea contents then i used compiler API but it is not compiling the file. I want to do something like this

index.html

  <form action="formAction.jsp" method="post">
        Please enter your text:
        <br/>
        <textarea name="textarea1" rows="5"></textarea>
        <br/>
        <input type="SUBMIT" value="Submit"/>
    </form>     

formAction.jsp

 <%

        String convert = request.getParameter("textarea1");
        PrintWriter wr = response.getWriter();
        File f =new File("file121.java");
        if(!f.exists())
        {
            f.createNewFile();
            wr.println("File is created\n");

        }
        FileWriter write = new FileWriter(f);
        BufferedWriter bf = new BufferedWriter(write);
        bf.write(convert);
        bf.close();
        wr.println("File writing done\n");

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int result = compiler.run(null,null,null,"file121.java");
        if(result==0){
            wr.println("Compilation Successful");
        }
        else{
            wr.println("Compilation Failed");
        }




     %>   

推荐答案

  1. 请勿使用脚本,请编写可从jsp调用的Java代码.
  2. Java需要一个带有类名的文件名,这可能是您的主要问题.
  3. 用户可以输入的Java代码的预期格式是什么?方法,类还是方法中的脚本?
  4. 具有您的Java知识水平,请不要允许未知用户输入代码,否则您的服务器很快就会被黑客入侵
  1. Please do not use scriptlets, write java code that you can call from your jsp.
  2. Java requires a file name with the name of the class, this was probably your main problem.
  3. What is the expected format of java code the user can enter? Method, Class or only a script inside a method?
  4. With your level of java knowledge, please do not allow unknown users to enter code or your server will get hacked very soon!

以下代码编译变量convert中的代码.它会自动检测输入的类的名称.如果未找到任何类,它将创建一个类名为MyClass的模板.

The following code compiles the code in variable convert. It automaticaly detects the name of the class entered. If no class has been found, it creates a template with the classname MyClass.

      String convert = "public class NewClass {  public static void main(String[] args) {    System.out.println(\"test\");  }}";
  String filename = "MyClass.java";

  int i = convert.indexOf(" class ");
  if(i == -1) {
    convert = "public class MyClass { " + convert + " } ";
  } else {
    int classNameIndex = convert.indexOf(" ", i+1);
    int classNameIndexEnd = convert.indexOf(" ", classNameIndex+1);        
    filename = convert.substring(classNameIndex+1, classNameIndexEnd) + ".java";
  }

  PrintWriter wr = response.getWriter();
  File f = new File(filename);
  if (!f.exists()) {
    f.createNewFile();
    wr.println("File is created\n");

  }
  FileWriter write = new FileWriter(f);
  BufferedWriter bf = new BufferedWriter(write);
  bf.write(convert);
  bf.close();
  wr.println("File writing done\n");

  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  int result = compiler.run(null, null, null, filename);
  if (result == 0) {
    wr.println("Compilation Successful");
  } else {
    wr.println("Compilation Failed");
  }

这篇关于如何在jsp中编译.java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆