简体   繁体   English

通过j2EE中的servlet通过main运行类

[英]running a class with main from a servlet in j2EE

How to (or can i) run a class with main() from a servlet?? 如何(或我可以)从servlet运行带有main()的类?

i want to take input from a html page and insert data into a database using hibernate. 我想从html页面获取输入,然后使用休眠将数据插入数据库。

-i have an annotated class. -我有一个带注释的类。

-a class having main(), running which inserts the data into the database. -运行main()的类,该类将数据插入数据库。

-and a servlet with post method code: -以及带有后方法代码的servlet:

String input_from_html=request.getParameter("input_from_html");

I need to put the data 'input_from_html' to the class with main() and run the class,which should happen once the submit button in the html page is pressed. 我需要使用main()将数据“ input_from_html”放入类中并运行该类,这将在按下html页面中的提交按钮时发生。

This is not a good idea, because you want your database insert to be within a transaction so it can either commit or rollback as appropriate. 这不是一个好主意,因为您希望将数据库插入到事务中,以便可以根据需要进行提交或回滚。 Running the insert as an executable triggered by a request to a servlet means there won't be any transaction that's initiated by the incoming request. 将插入文件作为由Servlet请求触发的可执行文件运行,这意味着传入请求不会启动任何事务。

Invoke the main like any normal static method. 像任何常规静态方法一样调用main。

For Ex, 对于前

class MainClass {
  public static void main(String args[]){

  }
}

You can call this main method inside servlet like : 您可以在servlet内部调用此主要方法,例如:

class AServlet extends HttpServlet {
  public void service(...){
      MainClass.main(...);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM