Run Java class from JSP

This is how my main java class looks like

     public class Main {

public static void main(String[] args) throws Exception {

    XmlParser b = new XmlParser();

    ServiceController sl = new ServiceController();

    Pipeline2 objPipeline2 = new Pipeline2();

    objPipeline2.main(args);

    b.parseXML();

    sl.callServiceByDomain();

}

public void function1() throws Exception {
    System.out.println("hello");

}

      

Here is the run.jsp file where I want to run the main function

<%@ page import="java.io.*" %>
<%@ page import="main.Main" %>

<HTML>
    <HEAD>
        <TITLE>Enter Email over here</TITLE>
    </HEAD>
    <BODY>
    <jsp:useBean id="link" scope="application" class = "main.Main" />   
  <%Main r=new Main();
  Main.main(null);%>
    </BODY>
</HTML>

      

I cannot run it if I run the run.jsp file where as if I was trying to run function1 from jsp instead of main ... it works. please help me in this regard

+3


source to share


1 answer


The main method takes a parameter ( String[] args

). Try <%r.main(null);%>

it if you are not interested in passing any parameters.



BTW, main

is static, so <%Main.main(null);%>

should work as well, and is the preferred way to call a static method (i.e. no instantiation main

).

+1


source







All Articles