Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run code before servlet class loading on Tomcat?

I'm doing some runtime bytecode manipulation on some of my business objects, and it's very important that they be loaded in the right order. Currently I'm simply calling Class.getSimpleName() on them in the right order in my Startup servlet. This has been working just fine, but if there's a better way, I'm all ears.

Now, however, I need a method in one of my servlet filters to return a concrete business object type. This is causing the classloader to load that particular business object class first (out of order) and things break.

What I'd like is to be able to run my getSimpleName() hack before any of my servlets or filters are loaded. Is there some place I can put code that runs before the classloader even loads my filters?

like image 965
Erik R. Avatar asked Aug 12 '26 02:08

Erik R.


1 Answers

Yes, you can.

Look at ServletContextListener.

like image 169
Skip Head Avatar answered Aug 13 '26 14:08

Skip Head



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!