简体   繁体   中英

running java servlets as independent process

I'm very new to java/tomcat, and wondering if it is possible to make a java servlet for tomcat that will start when tomcat itself starts, ie independently of any request received, and which will keep running till tomcat stops? Furthermore, could such a servlet have unlimited access to the machine on which it is running, or is it essentially sandboxed by tomcat?

I already have something similar in Python, where a main process running with root privileges starts a sub-thread that contains a simple web server. Any requests coming in are then able to make changes to the machine as root, on basis of the root privileges. The motivation is to speak to a microcontroller via a browser-based GUI available over an intranet (so security is not an issue, it just seems like a very easy way of allowing people to remotely control lab equipment). I'm curious if this could be done in the java/tomcat ecosystem...

Tomcat doesn't have a sandbox, it's not like a Java Applet. It has all the permissions of the user you use to run it. So if it's running as root user it will have root privileges (Please be careful).

Yes you can.

implement the method init() in your servlet, and add:

   <load-on-startup>1</load-on-startup>

in web.xml file.

If you add permissions on jvm on this host, you could access to host resources: http://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html

If I understand your question correctly you want a background process running that accepts requests and then does something to the server Tomcat is running on.

You don't need any background process. Tomcat itself will dutifully process any incoming HTTP requests then execute the appropriate servlet based on the configuration in web.xml (or configuration and/or convention of whatever framework you used to write your app).

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.

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