使用javascript的多个操作,一个表单和一个文本框

时间:2016-03-03 16:27:54

标签: javascript java ajax servlets

我在下面的表单中有时会执行第一个操作,有时会执行最后一个操作,而不是一个接一个地执行这两个操作。

形式:

<html>
    <head>
    </head>
    <body>
        <script language="Javascript">
         function OnButton1()
         {
             document.Form1.action = "http://localhost:8084/WorldCupFinal/finalsearching1";
             document.Form1.target="_blank";
             document.Form1.submit();
             document.Form1.action = "http://localhost:8084/SemiFinal/finalsearching1";
             document.Form1.target="_blank";
             document.Form1.submit();

         }
        </script>
        <form name="Form1" method="post" target="_blank">
            File Name <input type="text" name="t1" size="10" ><br />
            <INPUT type="button" value="Button1"
                   name=name onclick="OnButton1();" >
        </form>
    </body>
</html>

finalsearching1是java servlet程序,它根据动作(在我的本地主机以及其他IP地址中)接受输入作为文件名搜索某个系统中的文件。

但是这个动作只能由我的servlet执行一次。我想要 一个接一个地执行这两个动作,或者有什么方法可以同时执行它们,这需要花费更少的时间。这个场景由我执行2个不同的按钮(2个动作)。我想使用一个带有单个文本框的按钮来执行它,并且会发生多个操作。

我搜索了很多,我发现这可以使用AJAX执行,或者如果可能的话使用javascript执行。 你能帮我解决一下吗?

提前感谢您的关注。

这是我的finalsearching1程序代码:

import static java.awt.SystemColor.window;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import static java.io.FileDescriptor.out;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import static org.apache.tomcat.jni.Shm.buffer;
import upload.UploadDownloadFileServlet;

@WebServlet(urlPatterns = {"/finalsearching1"})
public class finalsearching1 extends HttpServlet {
    private static String name,name1;
    private  static int a=0,p=0;
    int count=0;
    static URL u;
    static final int BUFFER_SIZE = 1024*1024;
    static HttpServletResponse response;

    private static void setContentType(HttpServletResponse response) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    private static void doGet1(String m1, HttpServletRequest request, HttpServletResponse response) {

    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");


    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fileName = request.getParameter("t1");
        if(fileName == null || fileName.equals("")){
            throw new ServletException("File Name can't be null or empty");
        }
        File file = new File(request.getServletContext().getRealPath("")+File.separator+fileName);
        if(!file.exists()){
            throw new ServletException("File doesn't exists on server.");
        }
        System.out.println("File location on server::"+file.getAbsolutePath());
        ServletContext ctx = getServletContext();
        InputStream fis = new FileInputStream(file.getAbsolutePath());
        String mimeType = ctx.getMimeType(file.getAbsolutePath());
        response.setContentType(mimeType != null? mimeType:"application/octet-stream");
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        ServletOutputStream os       = response.getOutputStream();
        byte[] bufferData = new byte[1024*1024];
        int read=0;
        while((read = fis.read(bufferData))!= -1){
            os.write(bufferData, 0, read);
        }
        os.flush();
        os.close();
        fis.close();
        System.out.println("File downloaded at client successfully");
    }
    private static void sendFile(HttpServletResponse response,  File clientFileName) throws IOException
    {
        ServletOutputStream op = null;

        setContentType(response);
        response.setHeader("Content-Disposition", "attachment; filename=\"" + clientFileName + "\"");

    }
    private static ServletFileUpload uploader = null;

    public static void parseAllFiles(String parentDirectory,HttpServletRequest request,HttpServletResponse response) throws IOException, Exception  {


        PrintWriter pr=response.getWriter();
        response.setContentType("text/html");


        File[] filesInDirectory = new File(parentDirectory).listFiles();

        if(filesInDirectory != null){

            for (File f : filesInDirectory){
                if(f.isDirectory()){

                    parseAllFiles(f.getAbsolutePath(),request,response);
                }
                else if(name.equalsIgnoreCase(f.getName()))
                    {

                        pr.println("File Found with path  "+f.getParentFile()+"\\"+name);

                        String m1=f.getParentFile()+"\\"+name;

                        System.out.println("File to upload: " + m1);
                        response.setContentType("text/html");

                        File file = new File(m1);
                        System.out.println();

                        String ext = "."+FilenameUtils.getExtension(m1);
                        System.out.println(ext);
                        f = File.createTempFile("tmp",ext, new File("C:\\Users\\Pankaj\\Desktop\\JADE-all-4.3.3\\WorldCupFinal\\build\\web"));
                        String f1=f.getName();

                        FileInputStream in = null;
                        FileOutputStream out = null;

                        try {
                            in = new FileInputStream(m1);


                            out = new FileOutputStream(f);

                            int c;
                            while ((c = in.read()) != -1) {
                                out.write(c);
                            }
                            System.out.println(c+"  Hi");
                        }

                        finally {
                            if (in != null) {
                                in.close();
                            }
                            if (out != null) {
                                out.close();
                            }
                        }


                        pr.write("Hi..........");



                        pr.write("<br>");
                        pr.write("<a href=\"?t1="+f1+"\">Download "+name+"</a>");
                        pr.write("<br>");
                        a++;
                    }



            }
        }
    }



    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        processRequest(request, response);
        PrintWriter pr=response.getWriter();

        name=request.getParameter("t1");
        if(name == null || name.equals("")){
            throw new ServletException("File Name can't be null or empty");
        }



        System.out.println("name:          "+name);
        System.out.println("Starting the execution of a project  :");
        response.setContentType("text/html");
        File[] files = File.listRoots();
        for(File f : files){
            try {
                System.out.println(f.getPath());
                System.out.println("response:"+response);
                parseAllFiles(f.getPath(),request,response);
            } catch (Exception ex) {
                Logger.getLogger(finalsearching1.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        if(a==0)
            pr.println("File Not Found");

        pr.println("Starting the Execution in system 2nd");
    }



    public String getServletInfo() {
        return "Short description";
    }
}

0 个答案:

没有答案