title: Invoking a Java program in JSP page
body: <p>I am new to java. i'm trying to invoke a java function on a <code>jsp</code> page.The function is supposed to copy a file from one directory to another directory. Through the <code>Jsp</code> I have to browse ahd select a file.. after this,i have to click on run button then it should perform the operation I gave in the java program. How to call this java program in <code>jsp</code>..please help me..
  Java Code:</p>
  
  <pre><code>package copy;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  public class CopyDirectoryExample {
      public static void main(String[] args) {    
          File srcFolder = new File("D:\\CETAF");
          File destFolder = new File("C:\\ExecFolder");
  
           //make sure source exists
          if(!srcFolder.exists()){
              System.out.println("Directory does not exist.");
              //just exit
              System.exit(0);
          } else {
              try {
                  copyFolder(srcFolder,destFolder);
              } catch(IOException e) {
                  e.printStackTrace();
                  //error, just exit
                  System.exit(0);
             }
          }
  
          System.out.println("Done");
      }
  
      public static void copyFolder(File src, File dest) throws IOException {
          if(src.isDirectory()) {
  
              //if directory not exists, create it
              if(!dest.exists()){
                 dest.mkdir();
                 System.out.println("Directory copied from " 
                                + src + "  to " + dest);
              }
  
              //list all the directory contents
              String files[] = src.list();
  
              for (String file : files) {
                 //construct the src and dest file structure
                 File srcFile = new File(src, file);
                 File destFile = new File(dest, file);
                 //recursive copy
                 copyFolder(srcFile,destFile);
              }
          } else {
              //if file, then copy it
              //Use bytes stream to support all file types
              InputStream in = new FileInputStream(src);
              OutputStream out = new FileOutputStream(dest); 
              byte[] buffer = new byte[1024];
              int length;
              //copy the file content in bytes 
              while ((length = in.read(buffer)) &gt; 0){
                  out.write(buffer, 0, length);
              }
              in.close();
              out.close();
              System.out.println("File copied from " + src + " to " + dest);
          }
      }
  }
  </code></pre>
  
  <p>index.jsp</p>
  
  <pre><code> &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
   &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01                       Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
         &lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Display file upload form to the user&lt;/TITLE&gt; 
  &lt;/HEAD&gt;
  &lt;BODY&gt;
   &lt;FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
            &lt;center&gt;
                  &lt;table border="0" &gt;     
                  &lt;tr&gt;         
             &lt;center&gt;            
             &lt;td colspan="2" align="center"&gt;&lt;B&gt;UPLOAD THE FILE&lt;/B&gt;  
            &lt;center&gt;          
              &lt;/td&gt;      
              &lt;/tr&gt;      
              &lt;tr&gt;           
               &lt;td colspan="2" align="center"&gt;&lt;/td&gt;
                   &lt;/tr&gt;     
               &lt;tr&gt;          
            &lt;td&gt;&lt;b&gt;Choose the file To Upload:&lt;/b&gt;&lt;/td&gt;          
              &lt;td&gt;&lt;INPUT NAME="file" TYPE="file"&gt;&lt;/td&gt;
                    &lt;/tr&gt;    
                   &lt;tr&gt;           
                &lt;td colspan="2" align="center"&gt;&lt;/td&gt;
                   &lt;/tr&gt;     
                 &lt;tr&gt;           
                     &lt;td colspan="2" align="center"&gt;&lt;input type="submit"
                value="RUN"&gt;&lt;/td&gt;
                    &lt;/tr&gt;    
                    &lt;/table&gt;       
                            &lt;/center&gt;       
                         &lt;/FORM&gt;
                         &lt;/BODY&gt;
                &lt;/HTML&gt;
  </code></pre>
  
  <p>Upload.jsp</p>
  
  <pre><code>                  &lt;%@ page import="java.io.*"%&gt;
                   &lt;%      String saveFile = "";   
                          String contentType = request.getContentType();  
                    if ((contentType != null) &amp;&amp; (contentType.indexOf
                          ("multipart/form-data") &gt;= 0)) {            
                 DataInputStream in = new DataInputStream(request.getInputStream());     
                   int formDataLength = request.getContentLength();        
                      byte dataBytes[] = new byte[formDataLength];       
                           int byteRead = 0;        
                      int totalBytesRead = 0;       
                  while (totalBytesRead &lt; formDataLength) {       
                   byteRead = in.read(dataBytes, totalBytesRead, formDataLength);            
                totalBytesRead += byteRead;           
                    }           
                     String file = new String(dataBytes);        
                           saveFile = file.substring(file.indexOf("filename=\"") + 10);       
                             saveFile = saveFile.substring(0, saveFile.indexOf           ("\n"));           
                       saveFile = saveFile.substring(saveFile.lastIndexOf("\\")                           +1,saveFile.indexOf                    ("\""));       
                  int lastIndex = contentType.lastIndexOf("=");           
                      String boundary = contentType.substring(lastIndex+ 1,                         contentType.length());          
                        int pos;           
                     pos = file.indexOf("filename=\"");           
               pos = file.indexOf("\n", pos) + 1;         
                 pos = file.indexOf("\n", pos) + 1;          
                    pos = file.indexOf("\n", pos) + 1;          
              int boundaryLocation = file.indexOf(boundary, pos) - 4;     
                 int startPos = ((file.substring(0, pos)).getBytes()).length;          
               int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;          
                 saveFile = "D:/CETAF/Configuration/" + saveFile;         
                     File ff = new File(saveFile);          
                  FileOutputStream fileOut = new FileOutputStream(ff);          
                      fileOut.write(dataBytes, startPos, (endPos - startPos));           
            fileOut.flush();            
                              fileOut.close();%&gt;&lt;Br&gt;&lt;table border="2"&gt;
                  &lt;tr&gt;         
                           &lt;td&gt;&lt;b&gt;You have successfully upload the file by the name of:&lt;/b&gt;         
                            &lt;%                  
                         out.println(saveFile);                 
                       }        
                                %&gt;          
                            &lt;/td&gt;      
                            &lt;/tr&gt;
                           &lt;/table&gt;
  </code></pre>
  
  <p>The above two are my jsp files...if i click on run button,it should call that java file and perform dat action..how to call that java file??</p>