简体   繁体   English

用Java中的Servlet替换Sriplets

[英]Replacing Sriplets by Servlets in Java

I have a JPS page from which I want to execute a shell script placed on server: Below code works fine and "/tmp/Sample.sh" script is getting executed on server. 我有一个JPS页面,我想从中执行放置在服务器上的Shell脚本:下面的代码可以正常工作,并且在服务器上执行“ /tmp/Sample.sh”脚本。

Now I want to do 2 things: 现在我想做两件事:

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.

I'm new to these terminologies as my primary skill is not java. 我是这些术语的新手,因为我的主要技能不是Java。 I have readed theory of servlet but , not getting how exactly i can achieve above two functionality. 我已经阅读了servlet的理论,但尚未获得如何实现上述两个功能的确切信息。 Any help in achieving above two points would be greatly appreciated 对于实现以上两点的任何帮助将不胜感激

<%@ 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>

UPDATED CODE AS PER SUGGESTIONS: 根据建议的更新代码:

http://10.111.24.21:7001/Project_1/Test_1.jsp 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 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 page translates to servlet eventualy so ofc it is possible to do same thong that you did in servlet. Jsp页面最终会转换为servlet,因此可以像在servlet中一样进行处理。 If usage of JSP page is absolutely necessery you can do fallowing things: 如果绝对需要使用JSP页面,则可以采取以下措施:

  1. Create servlet follow tutorial on Create Servlet 创建Servlet遵循有关创建Servlet的教程
  2. create method to execute command 创建执行命令的方法
 private void ExampleMethod() { String unixCommand = "sh /tmp/Sample.sh"; Runtime rt = Runtime.getRuntime(); rt.exec(unixCommand); } 
  1. Call method from doGet 从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>"); } 
  1. Replace scriptlet in body of jsp with: 将jsp主体中的scriptlet替换为:

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

  2. Replace server name, location (localhost:8080) with youre values ... 用您的值替换服务器名称,位置(localhost:8080)...

  3. FORGET ALL OF THAT AS SERVLETS AND SCRIPTING IN HTML IS SO OLD AND OBSOLETE IT SHOULD NOT BE USED ANY MORE 忘记所有作为SERVLETS和HTML中的脚本已过时且过时的内容,不应再使用它

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

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