aspose file tools*
The moose likes JSP and the fly likes How to execute a java program in a html/jsp page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to execute a java program in a html/jsp page?" Watch "How to execute a java program in a html/jsp page?" New topic
Author

How to execute a java program in a html/jsp page?

Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105
Hi,

I need to write one html/jsp page, where there will be an text area to write any java program and when i click on the execute button it should compile that program and print the ouput down there. Please help me to do this. How to compile a java program in the html page and print the output ?

Awaiting your reply

Thanks,
Keerthi Kumar N


Cheers,
Keerthi Kumar N
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Submit the textarea to the server when the user clicks compile. Then compile the program from your servlet. (You can call Runtime.exec to run javac) You run it on the server as well - be careful the user can't do anything dangerous to your server - and send it back to the webpage.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105
Hi,

Please find my programs below:
JSP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Demo Page</title>
</head>
<body>
<form action="/CompileJavaProgramServlet" method="post">

Write a java program in the below given text area and click on Execute button:
<br><br>

<textarea rows="15" cols="50" name="program"></textarea><br><br>

<input type="submit" value="Execute">   
<input type="reset" value="Reset">

</form>
</body>
</html>


Servlet:
*******
package com.Demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CompileJavaProgramServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String program = request.getParameter("program");
try
{
Process process = Runtime.getRuntime().exec("javac");

}
catch (Exception e)
{
out.println("Exception "+e);
}

}
}

Please guide me how to execute the java program entered in the jsp page in this servlet.

Thanks,
Keerthi Kumar N


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
You'll need to save the contents of request parameter "program" to a file in a specific location, say "/path/to/my/directory/Program.java".

Then you need to pass that path, along with the directory where the class file should be stored to javac (started via Runtime.exec), like "javac -d /path/to/my/directory" /path/to/my/directory/Program.java" (assuming that the class is named "Program".

Then you can again use Runtime.exec to execute the program (assuming that it has a proper "main" method). Make sure to pass the correct classpath to "java", and to handle the streams properly, as described in the artcile "When Runtime.exec() won't".


Ping & DNS - my free Android networking tools app
 
Don't get me started about those stupid light bulbs.
 
subject: How to execute a java program in a html/jsp page?