我有一个.exe文件,它通过点击jsp页面上的按钮启动和停止。当我们点击jsp页面上的按钮时,.exe文件启动,其日志被放在.txt文件中。例如,我的txt文件位于C:\ Debug \ logfile.txt.My问题是,当我单击启动时,.exe文件正在启动,其数据显示在指定的日志文件中,但输出未打印在jsp页面上。如果停止.exe文件,日志既会显示在指定的日志文件中,也会显示在jsp页面上。请帮助我解决这个问题。下面是停止和启动.exe文件的代码片段。
用于启动exe文件的servlet:
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException {
try{
String browsefile = req.getParameter("browsefile");
if(browsefile == null || browsefile.equals("")){
throw new ServletException("File Name can't be
null or empty");
}
File file = new File("C:/lic/test.lic");
if(!file.exists()){
throw new ServletException("File doesn't exists
on server.");
}
res.setContentType("text/html;charset=UTF-8");
PrintWriter out = res.getWriter();
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\tools\\server\\grd.exe -l[+]
C:\\Debug\\logfile.txt" );
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(pr.getInputStream()));
BufferedReader input = new BufferedReader(stdInput);
String serverstarted="";
StringBuffer startOutput = new StringBuffer();
while((serverstarted = input.readLine()) !=
null){
startOutput.append
(serverstarted + "\n");
}
req.setAttribute("startOutput",
startOutput.toString());
req.getRequestDispatcher
("grdoptions.jsp").forward(req, res);
}catch (Throwable t)
{
t.printStackTrace();
}
finally {
} }
}
用于停止.exe文件的Servlet:
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException {
try{
res.setContentType("text/html;charset=UTF-8");
PrintWriter out = res.getWriter();
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd /c taskkill /F /IM grd.exe");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(pr.getInputStream()));
BufferedReader input = new BufferedReader(stdInput);
String stop="";
StringBuffer stopOutput = new StringBuffer();
while((stop = input.readLine()) != null){
//out.println(stop);
stopOutput.append(stop + "\n");
}
req.setAttribute("stopOutput",
stopOutput.toString());
req.getRequestDispatcher
("grdoptions.jsp").forward(req, res);
// out.println("Command has been stopped");
}catch (Throwable t)
{
t.printStackTrace();
}
finally {
} }
}
jsp代码:
<table cellspacing="5" cellpadding="1" border="0">
<tr>
<td>Start Server</td>
<td>
<div id="start">
<form action="start" METHOD="GET"
enctype="multipart/form-data">
<input type="file" name=browsefile size="150" />
<input type="submit" name="start"
value="StartServer" />
</form>
</div>
<div id="result">
<pre>
${requestScope.startOutput}
</pre>
</div>
</td>
</tr>
</table>
<BR>
<table cellspacing="5" cellpadding="1" border="0">
<tr>
<td>Stop Server</td>
<td>
<div id="stop">
<form action="stop" METHOD="GET">
<input type="submit" name="txtSubmit2"
id="txtSubmit" value="StopServer" />
</form>
</div>
<div id="result">
<pre>
${requestScope.stopOutput}
</pre>
</div>
</td>
</tr>
</table>
WEB.XML:
<servlet>
<servlet-name>Start</servlet-name>
<servlet-class>com.abc.def.lic.Start</servlet-class>
</servlet>
<servlet>
<servlet-name>Stop</servlet-name>
<servlet-class>com.abc.def.lic.Stop</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Start</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Stop</servlet-name>
<url-pattern>/stop</url-pattern>
</servlet-mapping>
我想到的是使用Process pr = rt.exec(“C:\ tools \ server \ grd.exe -l [+]
C:\ Debug \ logfile.txt“);
启动exe文件并在logfile.txt中打印数据,这会阻止输出在jsp页面上打印。如果我删除-l [+] C:\ Debug \ logfile.txt,它将在网页上打印输出。但是我需要在网页和logfile.txt上输出。任何建议都是适用的。