How can compile I `.java file` in jsp?
Asked Answered
J

2

-4

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




     %>   
Jannet answered 12/4, 2016 at 20:18 Comment(13)
This is wrong on every level. You should not be doing this.Neonate
Scriptlets are a bad idea, for one. Are you trying to allow people to post source and compile it in your JSP? Sorry, never used JSPs that way. I don't use JSPs at all anymore - too 90s.Neonate
This is really very strange. Could you step back a little and update your question with some background so we can see what you're trying to achieve?Surculose
Let's assume this has a noble purpose. Are you trying to compile source code submitted through a form on your site?Lukelukens
First lesson learned: do not direct the client to do any heavy lifting.Lukelukens
I had an on-line compiler once. A person who was deep into maintaining the Java Compiler warned that there were short code samples that could tie up a compiler for 30 minutes or more. I removed it from public view. I think the view of @Neonate and others in comments is a good lead. Don't do this.Stasny
@AndrewThompson Pls open the link mention in the question and tell me how they did thisJannet
"Pls open the link mention in the question.." I'm not following a link external to SO. BTW - just because someone has done something, does not necessarily make it a 'good idea'.Stasny
Whatever you do, there is no reason to use scriptlets. You can call an action in java and pass the entered javacode to the java backend and compile from the java backend. If you really want to use scriptlets, you are missing imports in your question.Bulbar
JSP is client-side, why would you want to run a java file from there?Volumeter
@AniMenon JSP compiles and executes on the server.Bulbar
@Bulbar it can, but try to keep the client lite.Volumeter
@AniMenon There is no general rule to keep clients lite. Both architectures exists. Thin Client and Rich Client and they have their pros and cons. JSP is a server side technology and this discussion is out of the scope of this question!Bulbar
B
12
  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 automatically 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");
}
Bulbar answered 15/4, 2016 at 11:55 Comment(0)
G
1

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..

Guay answered 19/4, 2016 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.