代码之家  ›  专栏  ›  技术社区  ›  Viking

请求调度程序从表单中删除所有单词,但首先删除

  •  0
  • Viking  · 技术社区  · 6 年前

    这是我的JSP:

    <div>
      <b>${reply}</b>
    </div>
    <form action="runCommand" method="post" name="myform">
      File:<input name="commandfile" type="text" size="10" value=${commandfile}> </input><br />
      Command:<input name="commandinput" type="text" size="10" value=${commandinput}> </input><br />
      No delete File: <input type="checkbox" name="no_del_file" value=${no_del_file} ><br>
      <input name="submit" type="submit" value="Submit" />
      <input name="Reset" type="reset" value="Reset" />
    </form>
    

    这是我的java servlet代码:

     public class RunCommand extends HttpServlet {
    
    /**
     * 
     */
    private static final long serialVersionUID = 5711290708294275382L;
    String commands = null;
    File dir = null;
    BufferedReader is = null;
    BufferedReader es = null;
    boolean retval = false;
    int testwin = 0;
    int fileok = 0;
    String completefilename = null;
    File filecheck = null;
    private BufferedReader outfile;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        request.setAttribute("commandfile", "commandfile");
        request.setAttribute("commandinput", "commandinput");
        request.setAttribute("no_del_file", "no_del_file");
        HttpSession session = request.getSession();
        ServletContext application = getServletContext();
        performTask(request, response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
    
        performTask(request, response);
    }
    
        private int getWin() {
        Process Checkprocess;
        try {
            String Checkcommand = "cmd /c tasklist /V /FI \"WINDOWTITLE eq Administrator:*\"";
            Checkprocess = Runtime.getRuntime().exec(Checkcommand);
            String Checkline;
            String Checkval = "Administrator:";
    
            is = new BufferedReader(new InputStreamReader(Checkprocess.getInputStream()));
            while ((Checkline = is.readLine()) != null) {
                retval = Checkline.contains(Checkval);
                if (retval = true) {
                    testwin = 1;
                    break;
                } else
                    testwin = 0;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return testwin;
        }
    
        private int getFile() {
    
        filecheck = new File(completefilename);
    
        while (fileok == 0) {
            if (filecheck.exists()) {
                fileok = 1;
                break;
            }
    
        }
        return fileok;
    
         }
    
        private void performTask(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String commandbuild = request.getParameter("commandinput");
            String commandfilename = request.getParameter("commandfile");
            String[] no_del_file = request.getParameterValues("no_del_file");
    
            final SimpleDateFormat datefor = new SimpleDateFormat("ddMMyyyy_HHmmss");
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            String timeadd = datefor.format(timestamp);
    
            completefilename = "C:\\tmp\\" + commandfilename + "_" + timeadd + ".txt ";
    
            String commandfull = "db2 -tvz " + completefilename + commandbuild;
    
            commands = "cmd /c db2cwadmin.bat " + commandfull;
    
            Process process;
                process = Runtime.getRuntime().exec(commands);
            String line;
            StringBuilder data = new StringBuilder();
    
            is = new BufferedReader(new InputStreamReader(process.getInputStream()));
            data.append("Befehl: " + commands);
            data.append("\n\n\n");
    
            while ((line = is.readLine()) != null) {
    
                data.append(line);
                data.append("\n");
    
            }
            es = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = es.readLine()) != null)
                System.err.println(line);
    
            int exitCode = -100;
            exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("It worked");
    
                while (testwin != 1) {
                    getWin();
                    if (testwin == 1) {
                        break;
                    }
                }
    
                while (fileok != 1) {
                    getFile();
                    if (fileok == 1) {
                        break;
                    }
                }
    
                Process EndProcess;
                String Endcommands = "taskkill /F /FI \"WINDOWTITLE eq Administrator:*\"";
                EndProcess = Runtime.getRuntime().exec(Endcommands);
                int exitCodeEnd = -100;
                exitCodeEnd = EndProcess.waitFor();
    
                while (exitCodeEnd != 0) {
                    EndProcess = Runtime.getRuntime().exec(Endcommands);
                    exitCodeEnd = EndProcess.waitFor();
                    if (exitCodeEnd == 0) {
                        break;
                    }
                }
    
                // TimeUnit.SECONDS.sleep(10);
    
                data.append("Fenster wurde geschlossen!");
                data.append("\n");
                data.append("\n");
    
                File filecheck = new File(completefilename);
                if (filecheck.exists())
                    outfile = new BufferedReader(new FileReader(completefilename));
                String outfilelines;
    
                while ((outfilelines = outfile.readLine()) != null) {
                    data.append(outfilelines);
                    data.append("\n");
    
                }
    
                request.setAttribute("data", data);
                out.append(data);
    
    
                outfile.close();
    
    
                if (no_del_file == null) {
                filecheck.delete();
                }
    
                request.setAttribute("commandfile", commandfilename);
                System.out.println(commandbuild);
                request.setAttribute("commandinput", commandbuild);
                request.setAttribute("no_del_file", no_del_file);
                HttpSession session = request.getSession();
                ServletContext application = getServletContext();
                RequestDispatcher dispatcher = request.getRequestDispatcher("/runCommand.jsp");
                dispatcher.forward(request, response);
    
            } else
                System.out.println("Something bad happend. Exit code: " + exitCode);
    
        } // try
        catch (Exception e) {
            System.out.println("Something when wrong: " + e.getMessage());
            e.printStackTrace();
    
        } // catch
    
        finally {
    
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                }
            if (es != null)
                try {
                    es.close();
                } catch (IOException e) {
                }
    
        } // finally
    
        retval = false;
        testwin = 0;
        fileok = 0;
        completefilename = null;
    
    }
    

    }

    问题是只设置了第一个单词。因此,如果命令(我用它填写命令输入)是:

     list applications
    

    我只得到

     list 
    

    返回到命令字段,并且在提交之前设置复选框时也未设置复选框。

    因此,在命令输入字段中,前进键会删除第一个空格后的所有单词,并且不会再次设置复选框(复选框不会保留)。我需要按照提交前的方式填写表格。

    有什么想法吗?

    谢谢

    1 回复  |  直到 6 年前
        1
  •  2
  •   Jonathan Laliberte    6 年前

    好吧,据我所知。。。当您在表单字段中输入文本“list applications”时,它将被提交到servlet,但它只返回“list”。

    查看servlet代码,问题不在后端。因此,它与前端的某些东西有关,这些东西正在改变输入。

    让我们通过查看表单实际发送到servlet的内容来检查这一点。您可以通过查看chrome developer tools/或firefox developer tools中的network(网络)选项卡来完成此操作。

    chrome开发者工具的流程如下:

    在页面上单击鼠标右键,然后单击“检查”。

    1. 转到“网络”选项卡。
    2. 选中“保留日志”框。(在顶部)
    3. 现在提交表单,同时保持此窗口打开。
    4. 在这些请求中查找servlet url(“RunCommand”),当您单击它时,另一个窗口将打开,在这里您将看到标题、预览、响应、cookie和计时。
    5. 确保您正在查看“标题”选项卡,然后一直向下滚动到“表单数据”。在这里,您可以看到表单提交给servlet的内容,这是一个非常有用的调试工具。请告诉我你在那里看到了什么。

    另一件值得尝试的事情是删除 size="10" 从您的输入字段。这限制了文本字段的宽度,可能与您的问题有关。