这是我的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
返回命令字段,并且在提交之前设置复选框时未设置复选框。
因此,在命令输入字段中,forward会删除第一个空白后的所有单词,并且不会再次设置复选框(不保留检查)。我需要在提交之前填写表格。
有什么想法吗?
由于
答案 0 :(得分:2)
好的,根据我的理解...当你在表单字段中输入文本“list applications”时,它会被提交给你的servlet,但它只返回“list”。
查看您的servlet代码,问题不在您的后端。所以它与前端的某些东西有关,这正在改变输入。
让我们通过查看您的表单实际发送到您的servlet来检查这一点。您可以通过查看chrome developer tools /或firefox开发人员工具中的网络选项卡来完成此操作。
Chrome开发人员工具的流程如下:
右键单击您的页面,然后单击“检查”。
另外值得尝试的是从输入字段中删除size="10"
。这会限制文本字段宽度,并可能与您的问题有关。