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

2019-09-29 17:41发布

问题:

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:

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


回答2:

There are several ways of implementing this scenario but we must not include Java code in the JSPs rather use Expression Language (EL) for the same or we can even write custom jsp tags for our custom functionality. This link will explain a lot about this process.

http://www.journaldev.com/2099/jsp-custom-tags-example-tutorial

Even though if you like going through text books then Head First Servlet and JSP explains this whole concept in detail and elegantly. So for having proper functioning information this book is a must have book.

Happy Learning..