【问题标题】:Run code from Java file in jsp?在jsp中从Java文件运行代码?
【发布时间】:2013-03-05 08:08:16
【问题描述】:

我有一个这样的java文件

    public class Execute {

    public void run(){
        try{
            Runtime runtimeInstance = Runtime.getRuntime();
            Process p = runtimeInstance.exec("cmd /c D:\\Data\\Personal\\abu\\CobaAppFuse\\ExecuteCLI\\src\\java\\cli\\abu.bat");
            System.out.print(p);

        }catch(Exception ex){
            System.out.print(ex);
        }


    }
}

我的jsp文件路径如何运行文件??

文件 JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="abu.execute.cli.Execute" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <table align="center" border="3">
            <tr>
                <td><input type="submit" value="submit" name="submit"/></td>
            </tr>
        </table>

    </body>
</html>

我想在按下提交按钮时运行 java 文件 谢谢……

【问题讨论】:

  • 我的jsp文件路径如何运行该文件?? -->详述。不清楚
  • 我没有投反对票,但由于对投反对票没有评论,我会这样做:您的问题缺乏质量,请阅读[如何提问][stackoverflow.com/questions/how-to-ask] 部分。你的问题需要把问题放到一个上下文中,问题要清楚,提供的代码要尽可能的小,同时还要完整。

标签: java jsp jakarta-ee servlets


【解决方案1】:

我猜你想在有人访问 JSP-File x.jsp 时执行该过程。

虽然可以在 JSP 文件中运行 Java 代码表单,但我绝不会推荐它。您可能会问为什么,但这比我在this 帖子中得到的记录要好。

要回答您的问题,您需要创建一个 servlet

@WebServlet("/YourServlet")
public class YourServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public YourServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      new Execute.run();
      request.getRequestDispatcher("/WEB-INF/_search.jsp").forward(request, response);
    }
}

此外,您还必须更改您的网络应用程序的权限。这记录在page

请注意,这是最基本的方法,而不是最干净的方法。通常任何 java 代码的执行(控制执行计划)都是在服务中完成的。你可以通过(agian)声明一个 HttpServlet 并在那里声明一个服务来向你的应用程序添加一个服务。

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        new Execute.run();

        if (view.equals(request.getPathInfo().substring(1)) {
            request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);
        } else {
            response.sendRedirect(view);
        }
    } catch (Exception e) {
        throw new ServletException("Executing action failed.", e);
    }
}

虽然我不知道下面的评论是什么意思,但我会将 html 代码添加到答案中以使其更完整。在实践中,这意味着:

在提交时,html 需要向 servlet 发送 HTTP-Post。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <table align="center" border="3">
            <tr>
                <td><form action='Myactionhandle'><input type="submit" value="submit" name="submit"/></form></td>
            </tr>
        </table>
    </body>
</html>

servlet 必须执行代码。

@WebServlet("/Myactionhandle")
    public class YourServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public YourServlet() {
            super();
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          new Execute.run();
          request.getRequestDispatcher("/WEB-INF/_search.jsp").forward(request, response);
        }
    }

【讨论】:

  • jsp文件给她当什么??因为我在jsp调用servlet文件的时候很困惑,??
  • @AbuYazidBustomi 我编辑了我的答案。但我真的建议您开始阅读有关 Java Web 开发的教程。
【解决方案2】:

在您的 JSP 中添加 javascript。单击按钮后,调用可以通过使用脚本创建对象来执行 java 类的函数。

【讨论】:

  • 不能通过添加 scritlet 从 javascript 调用 java 类方法。
猜你喜欢
  • 1970-01-01
  • 2013-09-06
  • 2015-07-29
  • 1970-01-01
  • 2018-05-02
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 2023-03-13
相关资源
最近更新 更多