我有一个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文件 谢谢....
答案 0 :(得分:1)
我猜你想要在有人访问JSP-File x.jsp时执行该程序。
虽然可以在JSP-File中运行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);
}
}
答案 1 :(得分:-1)
在JSP中添加javascript。单击该按钮,通过使用scriptlet创建对象来调用可以执行java类的函数。