There is a misconception here. JSP is a view technology which provides a template to write HTML/CSS/JS in and provides the ability to interact with backend Java code in flavor of taglibs and EL during the generating of the response. It can however be done with normal Java code. You could write that in a JSP file, but it's considered bad practice. Java code belongs in real Java classes. Your problem is actually to be split in 2 parts.
First the question how to execute the desired task using Java code. As answered several times before you need Runtime#exec()
for this. However, be sure that you've read and understood all the 4 pages of this article! Here's a kickoff example:
public class YourShellClass {
public Result execute() {
Runtime runtime = Runtime.getRuntime();
// ... implement
return result;
}
}
Now left behind the question how to invoke it using a HTML form which is served by a JSP page. Let's assume that the HTML form look like this:
<form action="run" method="post">
<input type="submit" value="run">
</form>
This form will send a POST request to http://example.com/somecontext/run
. To exectue Java code whenever this request arrives at your server, just create a class which extends HttpServlet
which listens on an url-pattern
of /run
and has the doPost()
roughly implemented as follows:
protected void doPost(HttpServletRequest request, HttpServletResposne response) throws ServletException, IOException {
YourShellClass yourShellClass = new YourShellClass();
Result result = yourShellClass.execute();
// ... do something with it?
// Then display some result page?
request.getRequestDispatcher("result.jsp").forward(request, response);
}