Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

How can compile I `.java file` in jsp?

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
");

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

        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");
        }




     %>   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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!

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
");

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...