从JSP运行一个java主类 [英] Run a java main class from JSP
问题描述
这就是我的java主类的样子
public class Main {
public static void main(String [] args)抛出异常{
XmlParser b = new XmlParser();
ServiceController sl = new ServiceController();
Pipeline2 objPipeline2 = new Pipeline2();
objPipeline2.main(args);
b.parseXML();
sl.callServiceByDomain();
$ b public void function1()抛出异常{
System.out.println(hello);
$ / code>
这里是我想运行的run.jsp文件主要功能
<%@ page import =java.io. *%>
<%@ page import =main.Main%>
< HTML>
< HEAD>
< TITLE>在此输入电子邮件< / TITLE>
< / HEAD>
< BODY>
< jsp:useBean id =linkscope =applicationclass =main.Main/>
<%Main r = new Main();
Main.main(null);%>
< / BODY>
< / HTML>
如果我正在运行run.jsp文件,我无法运行它,试图从jsp代替main运行function1 ....它正在运行。请在这方面帮助我。
主要方法接受一个参数( c $ c> main String [] args
)。如果您不想传递任何参数,请尝试
是静态的,所以<%r.main(null);%>
。
<%Main.main(null);%>
也应该工作,并且是调用静态方法的首选方式(即不创建 Main
的实例)。
This is how my java main 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 am not able to run it if i am running the run.jsp file where as if i am trying to run the function1 from the jsp in place of main....it is running. please help me in this regard
The main method accepts a parameter (String[] args
). Try <%r.main(null);%>
if you are not interested in passing any parameters.
BTW, main
is static, so <%Main.main(null);%>
should also work, and is the preferred way of calling a static method (i.e. without creating an instance of Main
).
这篇关于从JSP运行一个java主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!