gpt4 book ai didi

java - 在 Java 中用 Servlet 替换 Sriplet

转载 作者:行者123 更新时间:2023-12-02 10:52:47 25 4
gpt4 key购买 nike

我有一个 JPS 页面,我想从中执行放置在服务器上的 shell 脚本:下面的代码工作正常,并且“/tmp/Sample.sh”脚本正在服务器上执行。

现在我想做两件事:

1.The script is getting executed as soon as page is loaded, but i want to execute it only when button is clicked.

2.I understand that use of scriplets is discouraged, what I googled is that I should call a servlet, when button is clicked, in which i can move the java code.

我对这些术语很陌生,因为我的主要技能不是 java。我已经阅读了 servlet 理论,但是不知道如何实现上述两个功能。任何有助于实现上述两点的帮助将不胜感激

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title></title>
</head>
<body>
<%
String unixCommand = "sh /tmp/Sample.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);
%>
</body>
</html>

根据建议更新代码:

<强> http://10.111.24.21:7001/Project_1/Test_1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title></title>
</head>
<body>
<button onclick="location.href = 'http://10.111.24.21:7001/Project_1/JavaServletClass.java';" id="RedirectButton" > Execute</button>
</body>
</html>

<强> http://10.111.24.21:7001/Project_1/JavaServletClass.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class JavaServletClass extends HttpServlet
{
public void init() throws ServletException { }
private void ExampleMethod() throws IOException
{
String unixCommand = "sh /tmp/Sample.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ExampleMethod();
out.println("<h1>" + "Method Executed" + "</h1>");
}

public void destroy() { }
}

最佳答案

Jsp 页面最终会转换为 servlet,因此可以执行与 servlet 中相同的操作。如果确实需要使用JSP页面,您可以执行以下操作:

  1. 按照 Create Servlet 上的教程创建 servlet
  2. 创建执行命令的方法
private void ExampleMethod() {
String unixCommand = "sh /tmp/Sample.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);
}
  • 从 doGet 调用方法
  •    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    // Set response content type
    response.setContentType("text/html");

    // Call commands
    PrintWriter out = response.getWriter();
    ExampleMethod();
    out.println("<h1>" + Method Executed + "</h1>");
    }
  • 将 jsp 正文中的 scriptlet 替换为:

    <button onclick="location.href = 'http://localhost:8080/SERVLETNAME';" id="RedirectButton" > Execute</button>

  • 将服务器名称、位置 (localhost:8080) 替换为您的值...

  • 忘记所有这些,因为 Servlet 和 HTML 脚本已经过时且过时,不应再使用
  • 关于java - 在 Java 中用 Servlet 替换 Sriplet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52025710/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com