jsp+servlet,点击一个button,后台执行一个shell,用的是Runtime.getRuntime().exec()方法,执行shell的时候会随时返回命令行结果到前台页面用out=response.getWriter打印,怎么能实现同步把结果刷新显示的页面上,现在是只能等到shell进程都结束后才能显示
解决方案 »
- Ajax问题,救命啊!!!
- 。。。。。。。。。。。。。。。。。。网络通讯框架 Netty ,帮顶有分
- 项目中一个关于<object>标签显示的棘手问题
- 奇怪啊!<html:html>问题
- <jsp:useBean id="xxx" scope="session" class="接口"/> 可以这么用吗??
- java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
- 高分求解-解决另送100分
- 面试条件,大家来讨论
- servlet中的forward问题
- 用Lucene搜索引擎框架,怎样可以把这个技术在java web项目中应用?
- 初学者求助:getParameter和URL编解码的问题
- JSP这部分该如何实现 谢谢!
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String country = request.getParameter("countrylist");
String shellpath = path + shellname;
String shellstring = "sh" + " " + shellpath + " " + country;
logger.info("Shellstring:" + shellstring);
BufferedReader bufferedreader = null;
out = response.getWriter();
out.println("==========Monitor www.rhapsody.com task start up!==========");
try {
Process process = Runtime.getRuntime().exec(shellstring);
if(process != null){
bufferedreader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int exitValue = process.waitFor();
if(0 != exitValue){
out.println("Call shell failed. error code is :" + exitValue);
}
}else{
out.println("Call shell script failed.");
}
String line = "";
while ((line = bufferedreader.readLine()) != null){
out.println(line);
}
bufferedreader.close();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
out.println("==========Monitor www.rhapsody.com task Complete!==========");
out.flush();
out.close();
}
/*写了个简单的线程处理*/
public static void output(final InputStream in, final ServletOutputStream out){
new Thread(){
public void run(){
byte[] buf = new byte[1024];
int n=0;
try{
while((n=in.read(buf))!=-1){
out.write(buf, 0, n);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}