How to (or can i) run a class with main() from a servlet??
i want to take input from a html page and insert data into a database using hibernate.
-i have an annotated class.
-a class having main(), running which inserts the data into the database.
-and a servlet with post method code:
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.
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.
Invoke the main like any normal static method.
For Ex,
class MainClass {
public static void main(String args[]){
}
}
You can call this main method inside servlet like :
class AServlet extends HttpServlet {
public void service(...){
MainClass.main(...);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.