-1

I have a java file as this

    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);
        }


    }
}

how do I run the file when my jsp file path??

file 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>

I want to run java file when the submit button is pressed thank you....

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
  • *how do I run the file when my jsp file path??* --> Elaborate on this. Not clear – mtk Mar 05 '13 at 08:10
  • 1
    I didn't downvote, but since there is no comment on the downvote, I'll do that: your question is lacking quality, please read the [how to ask][http://stackoverflow.com/questions/how-to-ask] section. Your question needs to set the problem into a context, the problem has to be clear, and the code provided needs to be as small as possible while still being complete. – Sebastian van Wickern Mar 05 '13 at 08:26

2 Answers2

1

I'm guessing that you want to execute the procedure whenever someone accesses JSP-File x.jsp.

While it is possible to run Java code form inside a JSP-File, I would never recommend it. You might ask why, but this has been documented better than I ever could in this post.

To answer your question, you need to create a 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);
    }
}

Furthermore you will have to change the permissions of your webapp. Which is documented on this page.

Mind that this is the most basic approach, not the cleanest one. Usually execution of any java code (that controls the execution plan) is done within services. You can add a service to you application by (agian) declaring a HttpServlet and declaring a service there.

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);
    }
}

While I don't know what the comment below means, I'll add the html-code to the answer to make it more complete. In practice this would mean:

On submit the html needs to make an HTTP-Post to the servlet.

<!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>

And the servlet has to execute the code.

@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);
        }
    }
Community
  • 1
  • 1
Sebastian van Wickern
  • 1,699
  • 3
  • 15
  • 31
-1

Add javascript in your JSP. On click of the button, call the function where you can execute the java class by creating an object using scriptlets.

Aashray
  • 2,753
  • 16
  • 22