简体   繁体   中英

load java clases from outside web application

I have a servlet which takes in some parameters. One of the parameters include the class name which should do some operations based on the other parameters sent to the servlet. The class name parameter is dynamic so the servlet wouldn't know which class to load beforehand.

Since the classes change frequently (for maintenance) i need to keep the classes outside of the Application since I cannot restart server in the production environment. Also I need to keep in mind if the classes change the servlet needs to load up the most recent version of the class.What is the best option that i have.

  1. Use a rule engine.
  2. Use dynamic class loading but I am scared that after some there will be simply too many classes loaded in the memory and permgen space will suffer.

Please advise. Thanks in advance

This sounds like potentially dangerous in terms of security, but I am assuming you have taken steps to mitigate already.

The most straight-forward way, I believe, is to use dynamic class loading. Construct a URL classloader to load off a fixed file system location (make sure people cannot arbitrarily run any class they want!) load the class using that and execute the code (possibly via reflection as the class will not be visible on your web application's classloader) and properly dispose of the classloader and any objects created (such that there are no memory leak) As you mentioned, you might need to increase PermGen, but that goes for any solution that involves loading additional classes (which is part of your requirement)

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