Hai,
In my work my
servlet generates a
java class file say "abc.java" , this java file have to be compiled at runtime and this compiled class has to be used in the further execution with out stopping my servlet runner.
this Generated "abc.java" may change in every request.
I am using servlet runner and java web server 2.0.
`````````````````````````````````````````````````````
Just to
test this i have written a servlet testExe.java ,class getFileData.java ,GotoClass.java, WriteIntoFle.java
Initially i compile all of them and run the servlet, after that i make change in the GotoClass.java in str variable(now i don't compile it). and run the servlet.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class testExe extends HttpServlet{
public void init(ServletConfig config) throws ServletException {
super.init(config);
} // End of init()
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(" <html><head><title> Hai</title></head><body>");
String str = " Hello";
out.println("<b>"+str+"</b><br>");
getFileData gt = new getFileData();
String data = gt.getfiledata();
System.out.println(" File Received to Servlet from getFileData:\n");
System.out.println(" Data:\n"+data+"\n Data Complete");
out.println("data printing :<br>"+data+"<br> Data printing Completed");
WriteIntoFile wf = new WriteIntoFile();
wf.writefile("GotoClass.java",data);
try{
Process p = Runtime.getRuntime().exec ("javac GotoClass.java");
System.out.println("Compiled");
p.destroy();
System.out.println("Operation Complete");
System.out.println(" Before the use of the class");
GotoClass gc = new GotoClass();
String str1 = gc.gotoclass();
out.println("<b>gc.gotoclass :"+str1+"</b>");
}//try
catch(IOException ioe){
ioe.printStackTrace();
}// catch
out.println("</body></html>");
out.close();
}// end of doGet()
}// end of class testExe
----------------------------------------------------------------
import java.io.*;
public class getFileData{
public String getfiledata(){
String retdata = null;
try{
System.out.println("\n\n Execution in getFileData:");
FileInputStream in = new FileInputStream("GotoClass.java");
InputStreamReader isr = new InputStreamReader(in);
int ch=0;
StringBuffer buf = new StringBuffer();
while((ch = in.read())> -1){
buf.append((char)ch);
System.out.print((char)ch);
}
System.out.println("\n\n");
retdata = buf.toString();
System.out.print("printing file :\n"+retdata+"\n Data Complete\n");
}
catch (IOException e){
System.out.println(e.getMessage());
}
System.out.println(" Termination of getFileData:\n\n");
return retdata;
}// end of method
}// end of class
----------------------------------------------------------------
import java.io.*;
import java.util.*;
import java.net.*;
public class WriteIntoFile {
public void writefile(String filename, String data){
System.out.println("\n\nExecution in WriteIntoFile:\n");
System.out.println("File name:\t"+filename+"\t \n Data received :\n"+data);
String pathname ="f:/mtp/temp/files/"+filename;
System.out.println(" Path name :\t "+ pathname);
File f = new File(pathname);
String s=null;
if(f.exists()){
System.out.println("File is all ready there");
f.delete();
f = new File(pathname);
try{
boolean created=false;
created=f.createNewFile();
System.out.println("After the File Creation");
}// end of try
catch(IOException ioe){
System.out.println("File Creation Error:\t\n");
ioe.printStackTrace();
}// end of catch
if(f.exists()){
String s1 = f.getAbsoluteFile().getName();
s = f.getName();
System.out.println(" File Name Created :\t"+ s);
System.out.println(" s1:\t"+s1);
s1 = f.getPath();
System.out.println(" Path of the file :\t"+s1);
System.out.println(" File Url Name:\t"+s1);
try{
FileWriter fw = new FileWriter(s);
String str = data;
fw.write(str);
fw.flush();
fw.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}// end of if exists
}// end of if(f.exists())
else {
try{
boolean created=false;
created=f.createNewFile();
System.out.println("After the File Creation");
}// end of try
catch(IOException ioe){
System.out.println("File Creation Error:\t\n");
ioe.printStackTrace();
}// end of catch
if(f.exists()){
String s1 = f.getAbsoluteFile().getName();
s = f.getName();
System.out.println(" File Name Created :\t"+ s);
System.out.println(" s1:\t"+s1);
try{
FileWriter fw = new FileWriter(s);
String str = data;
fw.write(str);
fw.flush();
fw.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}// end of if exists
}// end of esle if(f.exists())
System.out.println("\nTermination of WriteIntoFile file\n\n\n");
}// end of writefile method
}// end of class WriteIntoFile
----------------------------------------------------------------
import java.io.*;
import java.util.*;
public class GotoClass {
public String gotoclass(){
//String str =" Before making change, <b>Initially compiled<br></b><br>No change in execution";
String str = " After the compilation, <b>Compiled at runtime<br></b> Changes Done, Congrats";
return str;
}
}
--------------------------------------------------------------
thanx
prabhakar.