无法启动从JSP .bat文件 [英] unable to start .bat file from JSP

查看:169
本文介绍了无法启动从JSP .bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件runthis.bat

I have a batch file runthis.bat

DIR> DIR.TXT

如果我双击此,正与名DIR.TXT创建一个文本文件

If I double click on this, a text file is being created with name dir.txt

现在我不得不使用JSP运行这个批处理文件。

Now I have to run this batch file using JSP.

<%
Runtime run =Runtime.getRuntime();
run.exec("C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/try/runthis.bat");
out.println("SUCCESS");
%>

我得到网页上的输出成功,但这个批处理文件没有运行。

I'm getting the output SUCCESS on a webpage but this batch file is not running.

请告诉我可能是什么问题?

Whats might be the problem?

推荐答案

首先,JSP是在错误作为这个地方。做一个真正的Java类。开始一个的Servlet 。有一个按钮,提交到一个servlet的形式。将这个code在的doPost()方法。让servlet的结果放在请求范围并将请求转发给JSP。让JSP显示结果。

First, JSP is the wrong place for this. Do it in a real Java class. Start with a Servlet. Have a form with a button which submits to a servlet. Put this code in the doPost() method. Let the servlet put the result in request scope and forward the request to a JSP. Let the JSP display the result.

二,学会运行#EXEC的陷阱()中的这篇文章。你的问题是,你不检查的结果,也不是错误流(因此永远不知道,如果该计划成功执行),以及你期待它在某种程度上同步运行与编码(而实际上在一个单独的线程中运行/处理)。你基本上是做一个射后不理,在code基本上不会跟踪程序的执行/终止以任何方式。

Second, learn the pitfalls of Runtime#exec() in this article. Your problem is that you aren't checking the result nor the error stream (and thus never knows if the program executed successfully) and that you are expecting that it somehow runs in sync with your coding (while it actually runs in a separate thread/process). You're basically doing a "fire and forget", the code is basically not tracking the execution/termination of the program in any way.

这有问题的方式无关JSP。在一个正常的Java类,这样做时,你将面临完全一样的问题。

This problem has by the way nothing to do with JSP. You would face exactly the same problem when doing so in a normal Java class.

这篇关于无法启动从JSP .bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆