简体   繁体   English

从jsp页面运行.exe文件的代码

[英]code to run .exe file from jsp page

I want to start a .exe file from jsp button ie, when I click a start button on jsp page, it should start the exe file. 我想从jsp按钮启动一个.exe文件,即,当我点击jsp页面上的一个开始按钮时,它应该启动exe文件。 I was able to write code in java with getruntime.exec() with main method and when I run the java code am able to start the .exe file and could view the result that the file is started in console. 我能够使用getruntime.exec()和main方法在java中编写代码,当我运行java代码时,我能够启动.exe文件,并可以查看文件在控制台中启动的结果。 I am not sure how to call this java class into jsp and run the exe file from there. 我不知道如何将这个java类调用到jsp中并从那里运行exe文件。 Could some one please help on this. 有人可以帮忙解决这个问题。

Firstly you need to be familiar with java servlets . 首先,您需要熟悉java servlet Here's some basic steps: 这是一些基本步骤:

1. Create a servlet to run the exe program 1.创建一个servlet来运行exe程序

package mycompany;

public ExeRunnerServlet extends HttpServlet {
  protected doGet(HttpServletRequest req, HttpServletResponse res) throws Exception {
    String cmdToRunExe = //..

    // Implement exe running here..

    PrintWriter writer = res.getWriter();
    writer.append("Command has been run");
  }
}

This servlet only service GET http method. 这个servlet只服务GET http方法。 Search online for the code on how to run exe in Java, eg: https://stackoverflow.com/a/10686041/179630 . 在线搜索有关如何在Java中运行exe的代码,例如: https//stackoverflow.com/a/10686041/179630

2. Map the servlet on you WEB-INF/web.xml deployment descriptor 2.在您的WEB-INF / web.xml部署描述符上映射servlet

<web-app>
  <servlet>
    <servlet-class>mycompany.ExeRunnerServlet</servlet-class>
    <servlet-name>ExeRunnerServlet</servlet-name>
  </servlet>

  <servlet-mapping>
    <servlet-name>ExeRunnerServlet</servlet-name>
    <url-pattern>/exerunner</url-pattern>
  </servlet-mapping>
</web-app>

This will map the above servlet into http://myhost/mywarname/exerunner 这会将上面的servlet映射到http://myhost/mywarname/exerunner

3. Link it from JSP 3.从JSP链接它

Finally on your JSP you can create a html link to above servlet that will execute your exe program. 最后在JSP上,您可以创建一个到上面的servlet的html链接,它将执行你的exe程序。 Assuming your jsp is located at http://myhost/mywarname/page.jsp : 假设您的jsp位于http://myhost/mywarname/page.jsp

<a href="exerunner">Run exe command</a>

Few words of warnings 几句警告

  • Creating a single servlet and mapping them manually into web.xml is considered an obsolete approach. 创建单个servlet并将它们手动映射到web.xml被认为是一种过时的方法。 Consider newer web framework such as Spring MVC / JSF. 考虑更新的Web框架,例如Spring MVC / JSF。
  • Calling exe from java is often regarded as a bad approach, especially if process forking is involved. 从java调用exe通常被认为是一种糟糕的方法,尤其是涉及过程分叉时。 I've seen cases where jvm took 800mb memory and the whole process had to be temporarily duplicated just to invoke exe command. 我见过jvm占用800mb内存的情况,整个过程必须暂时重复,只是为了调用exe命令。 Consider other integration approach such as web services. 考虑其他集成方法,例如Web服务。

将main方法更改为executeProgram等其他方法,然后从jsp调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM